我没有写RequestHandler的经验,也不知道有多难。
如果您的要求是向现有查询添加更多过滤器,我认为编写 SearchComponent 很容易。
您可以编写一个 CustomComponent 扩展 SearchComponent。在该自定义组件中,您可以覆盖 prepare 方法,如下所示:
public void prepare(ResponseBuilder responseBuilder) throws IOException {
BooleanFilter booleanFilter = new BooleanFilter();
TermsFilter termFilter = new TermsFilter(new Term("name", value));
booleanFilter.add(new FilterClause(termFilter, Occur.MUST));
/*
Create a filtered Query with the with the filter created and the actual query
*/
FilteredQuery query = new FilteredQuery(responseBuilder.getQuery(),booleanFilter);
// Set the new query into the response builder.
responseBuilder.setQuery(query);
}
准备好搜索组件后,您可以在 solrConfig.xml 中创建 searchComponent,如下所示:
<searchComponent name="customComponent" class="com.CustomComponent">
<lst name="parameterName">
<str name="key">value</str>
</lst>
</searchComponent>
然后您可以将其添加为现有请求处理程序中的最后一个组件
<arr name="last-components">
<str>customComponent</str>
<str>spellcheck</str>
</arr>