Proximity Search using phrases in Solr

2020-05-18 03:30发布

问题:

I use Solr's proximity search quite often to search for words within a specified range of each other, like so

"Government Spending" ~2

I was wondering is there a way to perform a proximity search using a phrase and a word or two phrases. Is this possible? If so what is the syntax?

回答1:

This appears to be "somewhat" doable. Consider this text:

This is more about traffic between Solr servers themselves 

"more traffic between solr" ~2

"more about between solr" ~2

Even if you change the order it works:

"more about solr between" ~2" ~2

But too far apart and it stops working:

"more about servers themselves" ~2

I think if that doesn't work, it would probably not be TOO hard to make a custom request handler that does this. I think you might need to define a new syntax, prehaps something like ("phrase one" "phrase two") ~2. I would guess that if you are shingling, and you create a Lucene query where there is a token of just "phrase one" and another of "phrase two" that have a certain proximity, i think it will work. (of course you will need to actually make the lucene java call, you can't just hand the query over (read this http://lucene.apache.org/java/2_2_0/api/index.html)).



回答2:

Out of the box I have discovered a way to perform a Solr proximity search using more then one word, or phrases, see below

eg. with 3 words:

"(word1) (word2) (word3)"~10

eg. with 2 phrases: (note the double quote needs to be escaped)

"(\"phrase1\") (\"phrase2\")"~10



回答3:

Since Solr 4 it is possible with SurroundQueryParser.

E.g. to query where "phrase two" follows "phrase one" not further than 3 words after:

3W(phrase W one, phrase W two)

To query "phrase two" in proximity of 5 words of "phrase one":

5N(phrase W one, phrase W two)