I want to set a criteria for solr query in spring application. Is there any way to prioritize my criteria first and then bring remaining records in regular order? I have tried using:
filterQuery.addCriteria(new Criteria("type").is("abc"));
query.addFilterQuery(filterQuery);
query.setRows(10); // records limit
return query;
Now if there are 5 matching records where type
is abc
, the solr will return me just these 5 records. But what I want is to fill the limit which is 10 and get the remaining records accordingly with out any criteria being set. So that I have 5 records according to the criteria and remaining records in a regular order.
I know this can not be possible with is
method of the criteria. Is there any method which could help me with my scenario?
Well there is a way in which this can be done but this is not a very efficient way for doing this. So in order to fetch those items, which match the criteria, first and then remaining items later on, you can query twice to solr. In the first query, you can set the criteria against which you want the data to be fetched Like you already have done:
filterQuery.addCriteria(new Criteria("type").is("abc"));
query.addFilterQuery(filterQuery);
query.setRows(10); // records limit
return query;
And then, you can again configure this criteria such that, this time, Solr should only bring those items which do not
match the criteria. You may do this like:
filterQuery.addCriteria(new Criteria("type").is("abc").not()); // .not() is the magic
query.addFilterQuery(filterQuery);
query.setRows(10); // records limit
return query;
In this way, you may achieve your desired result.
Again, Its not a very efficient way but it works.