user4372823
user4372823

Reputation: 53

Matching all values in a query for a multiValued field in SOLR

I have a string multiValued field in SOLR named languages and I want a query to return only exact matches where all of the languages from the query are in the multiValued field.

For example, lets say I have three documents:

 "languages": [
          "English",
          "Russian",
          "Swedish"
        ],

"languages": [
          "English",
          "Japanese",
          "Russian",
          "Spanish",
          "Thai"
        ],

"languages": [
          "English",
          "Spanish"
        ],

If I were to query for English and Russian I should only return the first two documents. Here are some examples of the queries used:

q=languages:"English" and languages:"Russian"

q=languages:("English" and "Russian")

q=languages:("English","Russian")

q=languages:("Russian" "English")

In all cases these return all records that have either English or Russian. I may be overlooking something obvious but I have searched around and found nothing that explains this behavior.

Upvotes: 5

Views: 5626

Answers (4)

Naresh Joshi
Naresh Joshi

Reputation: 4597

Above answers are good but in case you have mentioned "AND" or "OR" in your stopwords.txt file then solr will remove "AND" and "OR" from your query. We have done same thing in our project due to lots of AND and OR in data itself.

In this case you can use && (for AND) and || (for OR) as conditional parameters in your query.

Example: q=languages:"English" && "Russian"

Upvotes: 0

Scott Chu
Scott Chu

Reputation: 990

Though this might be a late answer, it'd still be good late answer:

Firstly, "and" must be uppercase "AND".
Secondly, ("terma", "termb") and ("terma" "termb") should be same thing. The meaning of "," or " " depends on what the default operator you sepcify in schema.xml.
Thirdly, I don't think it's possible that the result is either terma or termb if you did specify AND correctly in search string.
Fourthly, if you really want either...or... result, you can specify like this: languages:("terma" OR "termb").

Upvotes: 0

user2894821
user2894821

Reputation: 186

Make sure you write the AND in upper case letters. The query would look like this:

q=languages:"English" AND languages:"Russian"

You can also use + or - to negate a part of the query. For example if you want a document which has "English" as a language but not "Russian", you would use a query like this:

q=+languages:"English" AND -languages:"Russian"

Upvotes: 2

Paweł Róg
Paweł Róg

Reputation: 126

Please try to add &defType=lucene to URL which you use.

Upvotes: 0

Related Questions