我以驯服文字一书为例,在solr中添加自定义QueryResponseWriter
。但它未在wt
中显示为tah
当我点击以下查询时,会显示空响应
http://localhost:8983/solr/collectiona/type-ahead?q=fayes&wt=tah
在我的solr配置中,我已经放置了我的lib的dir路径
<lib dir="${solr.install.dir:../../../..}/contrib/customresponsewriter" regex=".*\.jar" />
我已经为本书创建了一个jar文件。这是我的java类
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");
}
}
这是我的queryResponsewriter
的solr配置<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>
在solr启动期间加载自定义库,这是我的Solr日志
从路径中向类加载器添加了63个库:[/ home / bibek / software / java / solr-7.3.0 / conttrib / clustering / lib,/ home / bibek / software / java / solr-7.3.0/contrib / customresponsewriter,/ home / bibek / software / java / solr-7.3.0 / contrib / extrax / 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 / resist]
答案 0 :(得分:1)
在solr-core 7.3 NameList::getVal(index)
将返回BasicResultContext
,其中将包含DocList
个实例。以下是我更改后的代码以供将来参考
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");
}
}
}