How do I create a Solr query that returns results even if one field in my query has no matches?

StackOverflow https://stackoverflow.com/questions/22950132

  •  30-06-2023
  •  | 
  •  

Question

Suppose I want to create a recommendation system to suggest people you should connect with based off of certain attributes that I know about you and attributes I have about other people that are stored in a Solr index. Is it possible to query the index with a list of attributes (along with boosts for each attribute) and have Solr return scored results even if some of my fields return no matches? The way that I understand that Solr works is that if one of your fields doesn't contain a match in any documents found in your index, you get zero results for the entire query (even if other fields in the query matched) - is that right? What I would hope is that I could query the index and get a list of results back in order of a score given based on how many (and which) fields matched to something, even if some fields have no matches, for example:

Say that there are 2 people documents stored in the index as follows (figuratively):

Person 1:
     Industry: Manufacturing
     City: Oakland

Person 2:
     Industry: Manufacturing 
     City: San Jose

And say that I perform a pseudo-Solr query that basically says "Search for everyone whose industry is equal to manufacturing and whose city is equal to Oakland". What I would like is to receive both results back in the result set, even though one of the "Persons" does not reside in Oakland. I just want that person to come back as a result with a lower score than Person1. Is this possible? What might a solr query look like to handle this? Assume that I have many more than 2 attributes for each person (so saying that I can use "And" and "Or" in my solr query isn't really feasible.. or is it?) Thanks in advance for your helpful input! (PS I'm using Solr 3.6)

Was it helpful?

Solution

You mention using the AND operator, which is likely your problem.

The default behavior of Lucene, and Solr, query syntax is exactly what you are asking for. A query like:

industry:manufacturing city:oakland

Will match either, with scoring preference on those that match both. See the lucene query syntax documentation

OTHER TIPS

You can use the bq parameter (boost query) does not affect matching, but affects the scores only.

http://localhost:8983/solr/persons/select?q=industry:manufacturing&bq=City:Oakland^2

play with the boosting factor at the end to get the correct balance between matching score, and boosting score.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top