I save data in Solr:
String solrUrl = "http://localhost:8984/solr";
SolrServer solrServer = new HttpSolrServer( solrUrl );
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1");
doc.addField("first_name", "Ann");
doc.addField("last_name", "Smit");
doc.addField("email", "test@test.com");
try {
solrServer.add(doc);
solrServer.commit();
} catch (SolrServerException e) {/* */}
And I want to search in solr:
SolrQuery parameters = new SolrQuery();
parameters.setQuery("*:*");
parameters.setParam("first_name", "Ann");
QueryResponse response = null;
try {
response = solrServer.query(parameters);
} catch (SolrServerException e) {/* */ }
SolrDocumentList list = response.getResults();
And I have 1 records - it's good. But when I set parameters.setParam("first_name", "Ann1");
I have the same result. What is wrong?
parameters.setQuery("*:*");
will add q=*:*
to url, and this query matches all documents in solr(because its * wildcard char in both field:value).
parameters.setParam("first_name", "Ann");
has no effect because this will add first_name=Ann
query param to url. (Note that your filters should go as value to q
or fq
query param and not directly as separate query param!)
So, the final query will be q=*:*&first_name=Ann
which in lucene's language is to query for all docs.
If you are trying to match only the documents with first_name==Ann
then you need try ?q=first_name:Ann
SolrQuery parameters = new SolrQuery();
parameters.setQuery("first_name:Ann");
Search works based on your query in setQuery(":");
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setFields("id","first_name","last_name");
query.setStart(0);
query.setRows(100);
QueryResponse response = server.query(query);
SolrDocumentList docResults = response.getResults();
Your query should be in setQuery method and setFields() will make search on specified fields only.
Your are searching *:* so it return all documents. setParam("first_name","Ann") has no effect. Change your code like below.
SolrQuery query = new SolrQuery();
query.setQuery("first_name:Ann1");
Should use:
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.addFilterQuery("first_name:qwe*", "last_name:qwe*");
query.setFields("id","first_name","last_name","email");
QueryResponse response = null;
try {
response = solrServer.query(query);
} catch (SolrServerException e) {e.printStackTrace();}
SolrDocumentList list = response.getResults();