我有这样的组件:
public class TempComponent extends SearchComponent {
private static final Logger LOG = LoggerFactory.getLogger(SearchComponent.class);
@Override
public void prepare(ResponseBuilder rb) throws IOException {
LOG.info("Temp Component ---");
SolrIndexSearcher indexSearcher = rb.req.getSearcher();
IndexReader reader = indexSearcher.getIndexReader();
SolrParams params = rb.req.getParams();
String q = params.get(CommonParams.Q);
Query query = new TermQuery(new Term("name", q));
DocList matchDocs = indexSearcher.getDocList(query, null, null, 0, 100000,10000);
}
@Override
public void process(ResponseBuilder rb) throws IOException {
}
@Override
public String getDescription() {
return null;
}
}
在schema中,我为字段name设计了新的fieldType.<fieldType name="nameType" class="solr.TextField" positionIncrementGap="100" multiValued="false">
<analyzer type="index">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<field name="name" type="nameType" multiValued="false" indexed="true"/>
如何访问这个字段类型的分析器,并将其应用于查询(q)做搜索索引之前?
public class TempComponent extends SearchComponent {
private static final Logger LOG = LoggerFactory.getLogger(SearchComponent.class);
@Override
public void prepare(ResponseBuilder rb) throws IOException {
LOG.info("Temp Component ---");
SolrIndexSearcher indexSearcher = rb.req.getSearcher();
IndexReader reader = indexSearcher.getIndexReader();
SolrParams params = rb.req.getParams();
String q = params.get(CommonParams.Q);
Analyzer analyzer = rb.req.getSchema().getField("name").getType().getQueryAnalyzer();
StandardQueryParser standardQueryParser = new StandardQueryParser();
standardQueryParser.setAnalyzer(analyzer);
Query query = standardQueryParser.parse(q, "name");
DocList matchedCategories = indexSearcher.getDocList(query, null, null, 0, 100000,10000);
}
@Override
public void process(ResponseBuilder rb) throws IOException {
}
@Override
public String getDescription() {
return null;
}
}
我找到答案了!这很容易。