query for document that two fields are equal?

前端 未结 3 1401
有刺的猬
有刺的猬 2021-01-13 16:07

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



        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 16:43

    Method 1 - frange parser

    As mentioned by @dduo you can use the https://lucene.apache.org/solr/guide/6_6/other-parsers.html#OtherParsers-FunctionRangeQueryParser. Here's the way Trey Grainger (one of the authors of Solr in Action) said to do it:

    q=*:*&fq={!frange l=1 u=1 v=$equals}&equals=if(eq(field1,field2),1,0)
    

    I tested this and it worked for a collection with 140 million documents in about 10 second query with 600,000 in the result set.

    So this works, but it's kinda slow.

    Method 2 - Use a streaming expression

    The following expression seems to work to do what we are looking to do here:

    having(search(your_collection_name, q="*:*", sort="id asc"), eq(field1, field2))
    

    This seems to be much more performant, as it returns instant results. So if you can use streaming expressions, this is probably a faster way to get what you are looking for.

提交回复
热议问题