Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proximity Search using phrases in Solr

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?

like image 627
Ruth Avatar asked Jun 02 '10 11:06

Ruth


People also ask

What is phrase search in solr?

PhraseQuery in Lucene matches documents containing a particular sequence of terms. PhraseQuery uses positional information of the term that is stored in an index. The number of other words permitted between words in query phrase is called “Slop“. We can use the tilde, “~”, symbol at the end of our Phrase for this.

How do you escape special characters in solr?

Solr queries require escaping special characters that are part of the query syntax. Special characters are: +, -, &&, ||, !, (, ), ", ~, *, ?, and : . To escape these characters, use a slash ( \ ) before the character to escape.

How do you use FQ in solr?

In the lucid works SOLR training they suggested you do multiple fq parameters instead of a single one joined with AND for performance reasons. So in the sample it would be q=*:*&fq=(catid:90 OR catid:81)&fq=priceEng:[38 TO 40]&fq=.... etc.


2 Answers

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

like image 55
Ruth Avatar answered Sep 30 '22 01:09

Ruth


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)
like image 39
Andrey Avatar answered Sep 30 '22 01:09

Andrey