Vasily802
Vasily802

Reputation: 1858

Solr OR query on a text field

How to perform a simple query on a text field with an OR condition? Something like name:ABC OR name:XYZ so the resulting set would contain only those docs where name is exactly "XYZ" or "ABC" Dug tons of manuals, cannot figure this out. I use Solr 5.5.0

Update: Upgraded to Solr 6.6.0, still cannot figure it out. Below are illustrations to demonstrate my issue:

  1. This works:

works

  1. This works too:

works too

  1. This still works:

still works

  1. But this does not! Omg why!?

does not work omg why

Upvotes: 0

Views: 2005

Answers (3)

Vasily802
Vasily802

Reputation: 1858

A wacky workaround I've just come up with:

enter image description here

Upvotes: 0

MatsLindh
MatsLindh

Reputation: 52912

Your syntax is right, but what you're asking for isn't what text fields are made for. A text field is tokenized (split into multiple tokens), and each token is searched by itself. So if the text inserted is "ABC DEF GHI", it will be split into three separate tokens, namely "ABC", "DEF" and "GHI". So when you're searching field:ABC, you're really asking for any document that has the token "ABC" somewhere.

Since you want to perform an exact match, you want to query against a field that is defined as a string field, as this will keep the value verbatim (including casing, so the matching will be case sensitive). You can tell Solr to index the same content into multiple fields by adding a copyFile instruction, telling it to take the content submitted for field foo and also copying it into field bar, allowing you to perform both an exact match if needed and a more general search if necessary.

If you need to perform exact, but case insensitive, searches, you can use a KeywordTokenizer - the KeywordTokenizer does nothing, keeping the whole string as a single token, before allowing you to add filters to the analysis chain. By adding a LowercaseFilter you tell Solr to lowercase the string as well before storing it (or querying for it).

You can use the "Analysis" page under the Solr admin page to experiment and see how content for your field is being processed for each step.

After that querying as string_field:ABC OR string_field:XYZ should do what you want (or string_field:(ABC OR XYZ) or a few other ways to express the same.

Upvotes: 1

Sanjay Dutt
Sanjay Dutt

Reputation: 2222

There are many ways to perform OR query. Below I have listed some of them. You can select any of it.

[Simple Query]

q=name:(XYZ OR ABC)

[Lucene Query Parser]

q={!lucene q.op=OR df=name v="XYZ ABC"}

Upvotes: 1

Related Questions