I'm try开发者_StackOverflow社区ing to use the TermsComponent to implement autosuggest with Solrj, but I don't see how to specify the path (i.e. the /terms
portion of http://localhost:8983/solr/terms
).
How can I specify the path using Solrj?
Bonus: is there a way to specify multiple fields for the terms.fl
param?
Thanks
Here we go:
SolrQuery query = new SolrQuery();
query.setParam(CommonParams.QT, "/terms");
query.setParam(TermsParams.TERMS, true);
query.setParam(TermsParams.TERMS_LIMIT, "10");
query.setParam(TermsParams.TERMS_FIELD, "title", "description"); // or whatever fields you want
query.setParam(TermsParams.TERMS_PREFIX_STR, typedInput);
This is assuming that you have the TermsComponent wired in at "/terms"; the default solrconfig.xml has it there.
And for the bonus: you can add multiple fields simply by adding multiple strings for TERMS_FIELD
(or multiple URL &terms.fl=foo
params).
Thank you Mauricio, for pointing me in the right direction.
Direct support for TermsComponent is not available in SolrJ 1.4.1, but take a look at the corresponding patch, it's pretty easy to implement it yourself.
Since TermsComponent is a standard component you don't need to use /terms, you can hook it to the standard query request handler.
I spend so much time to make the TermsComponent work.
At the end of the day, I realized that there is a different set of commands that you need to invoke in order to retrieve the results from the "/terms" requestHandler.
I was trying to get the results using
HttpSolrServer server = new HttpSolrServ(solrUrl);
List<SolrDocument> list = server.query(query).getResults()
However, the correct way to get the results from the TermsComponent is
HttpSolrServer server = new HttpSolrServ(solrUrl);
TermsResponse termResp = server.query(query).getTermsResponse();
List<Term> tList = termResp.getTerms("fieldNAME");
I hope this may help someone else in the future.
精彩评论