mlissner
mlissner

Reputation: 18166

Search only for single value of Solr multivalue field, not across the values

I have a multivalued field in my schema called citation. One of the documents in the database has values for this field like:

 "citation":["13-33",
             "12-44"],

I want to be able to do a query like: citation:(13 44) and not have this document returned. In other words, I do not want queries to span individual values for the field.

Is there a way to do this?


Some further examples using the document above of how I want this to work:

Upvotes: 0

Views: 2037

Answers (3)

brendanh
brendanh

Reputation: 76

I think you can solve this with the correct field type and tokenisation for the citation field. If you use a field type like this:

<fieldType name="citation" class="solr.TextField" positionIncrementGap="100">
 <analyzer type="index">
   <tokenizer class="solr.KeywordTokenizerFactory"/>
   <filter class="solr.PatternCaptureGroupFilterFactory" 
           pattern="([0-9]+)-[0-9]+" preserve_original="true"/>
 </analyzer>
</fieldType>

Then your example document will be indexed thus:

"citation":["13", "13-33", "12", "12-44"]

This means the document will match on citation:"13" and citation:"13-33", but not citation:"13-12" or citation:"13-44"

Upvotes: 0

Alexandre Rafalovitch
Alexandre Rafalovitch

Reputation: 9789

SurroundQueryParser is your best bet for figuring out whether two terms are in the same value of a multiValued field.. The multivalued fields are actually internally one long set of tokens but with a big gap between tokens that belong to different "values". That's controlled by positionIncrementGap parameter in schema.xml, and is usually 100. So, setting the maximum gap to below 100 would require both terms to be within one field value.

Upvotes: 1

Zhitao Yue
Zhitao Yue

Reputation: 219

Solr doesn't support this kind of query, but perhaps you can try block join to achieve it. https://cwiki.apache.org/confluence/display/solr/Other+Parsers#OtherParsers-BlockJoinQueryParsers

Upvotes: 0

Related Questions