0
votes

I m taking taming text book as an example to add custom QueryResponseWriter in solr. But it is not shown in wt as tah

When I hit following query, empty response is shown

http://localhost:8983/solr/collectiona/type-ahead?q=fayes&wt=tah

In my solr config, I have placed following dir path of my lib

<lib dir="${solr.install.dir:../../../..}/contrib/customresponsewriter" regex=".*\.jar" />

I have created a jar file for this as mentioned in the book. Here is my java class

public class TypeAheadResponseWriter implements QueryResponseWriter {
        private Set<String> fields;


        public void write(Writer writer, SolrQueryRequest solrQueryRequest, SolrQueryResponse solrQueryResponse) throws IOException {
            SolrIndexSearcher searcher = solrQueryRequest.getSearcher();
            NamedList namedList = solrQueryResponse.getValues();
            int size = namedList.size();
            for (int i = 0; i < size; i++) {
                Object val = namedList.getVal(i);
                if (val instanceof DocList) {
                    DocList docList = (DocList) val;
                    DocIterator docIterator = docList.iterator();
                    writer.append("<ul>\n");
                    while (docIterator.hasNext()) {
                        int id = docIterator.nextDoc();
                        Document doc = searcher.doc(id, fields);
                        String name = doc.get("title");
                        writer.append("<li>" + name + "</li>");
                    }
                    writer.append("</ul>\n");
                }
            }
        }


        public String getContentType(SolrQueryRequest solrQueryRequest, SolrQueryResponse solrQueryResponse) {
            return "text/html;charset=UTF-8";
        }


        public void init(NamedList namedList) {
            fields = new HashSet<String>();
            fields.add("title");
        }
}

Here is my solr config for queryResponsewriter

<queryResponseWriter name="tah" class="cqw.TypeAheadResponseWriter"/>
   <requestHandler name="/type-ahead" class="solr.SearchHandler">
    <lst name="defaults">
     <str name="wt">tah</str>
     <str name="defType">dismax</str>
     <str name="qf">title_prefix_typeahead^1.0</str>
 </lst>
</requestHandler>

custom library is loaded during startup of solr, here is my Solr log

Added 63 libs to classloader, from paths: [/home/bibek/software/java/solr-7.3.0/contrib/clustering/lib, /home/bibek/software/java/solr-7.3.0/contrib/customresponsewriter, /home/bibek/software/java/solr-7.3.0/contrib/extraction/lib, /home/bibek/software/java/solr-7.3.0/contrib/langid/lib, /home/bibek/software/java/solr-7.3.0/contrib/velocity/lib, /home/bibek/software/java/solr-7.3.0/dist]

1
So how are you enabling the configuration? If you're using collections, it sounds like you're running Solr in cloud mode - and in that case you have to upload and update the configuration in the embedded zookeeper instance.MatsLindh
@MatsLindh I am using solr core and i have configure solr as classic schema.xml typeBibek Shakya
Do you see the jar being loaded in the server log?MatsLindh
What happens if you try to use the response writer? An exception? Nothing? XML output? What is your query?MatsLindh
Ah, sorry, missed the query - but have you tried adding some debug information to the log? If there is no entries or your val isn't a DocList (which you expect - maybe it isn't?) you won't add anything to the writer. Either attach your normal debugger to the process, use the available logging or at least add print-statements (these are usually included in the log file, depending on your log level). Since it's not complaining and returning nothing, it's probably running but not triggering any of the writes.MatsLindh

1 Answers

2
votes

In solr-core 7.3 NameList::getVal(index) will return BasicResultContext which will content DocList instance. Here is my changed code below for future reference

public void write(Writer writer, SolrQueryRequest solrQueryRequest, SolrQueryResponse solrQueryResponse) throws IOException {
    LOGGER.info("Here we are....");
    SolrIndexSearcher searcher = solrQueryRequest.getSearcher();
    NamedList namedList = solrQueryResponse.getValues();
    LOGGER.info("Named List " + namedList.size());
    int size = namedList.size();
    for (int i = 0; i < size; i++) {
        Object val = namedList.getVal(i);
        LOGGER.info(val.toString());
        if (val instanceof BasicResultContext) {
            BasicResultContext basicResultContext = (BasicResultContext) val;
            DocList docList = basicResultContext.getDocList();
            LOGGER.info("docList List " + docList.size());
            DocIterator docIterator = docList.iterator();
            writer.append("<ul>\n");
            while (docIterator.hasNext()) {
                int id = docIterator.nextDoc();
                LOGGER.info("id id " + id);
                Document doc = searcher.doc(id, fields);
                for (String field : fields) {
                    String value = doc.get(field);
                    LOGGER.info(value);
                    if (!StringUtils.isEmpty(value)) {
                        writer.append("<li>" + value + "</li>");
                    }
                }
            }
            writer.append("</ul>\n");
        }
    }
}