query for document that two fields are equal?

2019-02-25 09:21发布

问题:

There are two text fields in solr, both of them are white space tokenized and have lower case filter. below is the schema:

<fieldType name="text_ac" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="index">
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

<field name="field1" type="text_ac" indexed="true" stored="true" required="false" omitNorms="true" default=""/>

<field name="field2" type="text_ac" indexed="true" stored="true" required="false" omitNorms="true" default=""/>

How to query solr to return results that the whole string of field1 is same as field2 at query time (field1==field2)?

Thanks.

回答1:

Comparing one field to another within a document is not supported in either Solr or Lucene core, to my knowledge.

A simple way to accomplish this would be to perform the comparison at index time, and store the result in the index. That is, if you have field1 and field2, create a field 1_equals_2, and index it with true, if they are equal based on your comparison when adding the document. Then you can simply search for 1_equals_2:true.



回答2:

Have you tried the function 'strdist' and range query 'frange'? A range query like this would help:

{!frange l=1 u=1}strdist(field1, field2, edit)



标签: solr lucene