Felix Martinez
Felix Martinez

Reputation: 3978

Proximity Search example Lucene.Net

I want to make a Proximity Search with Lucene.Net. I saw this question where it looks like that was the answer for him, but no code was suplied. The Java documentation says to use the ~ character with the number of words in between, but I don't see where this character would go in the code. Anyone can give me an example of a Proximity Search using Lucene.Net?

Edit:

What I have so far:

IndexSearcher searcher = new IndexSearcher(this.Directory, true);

string[] fieldList = new string[] { "Name", "Description" };

List<BooleanClause.Occur> occurs = new List<BooleanClause.Occur>();
foreach (string field in fieldList)
{
     occurs.Add(BooleanClause.Occur.SHOULD);
}


Query searchQuery = MultiFieldQueryParser.Parse(this.LuceneVersion, query, fieldList, occurs.ToArray(), this.Analyzer);

If I try to add the "~" with any number on the MultiFieldQueryParser it errors out saying that for a FuzzySearch the values should be between 0.0 and 1.0, but I want a Proximity Search 3 words of separation Ex. "my search"~3

Upvotes: 3

Views: 2641

Answers (2)

sisve
sisve

Reputation: 19781

The tilde means either a fuzzy search if you apply it on a single term, or a proximity search if you apply it on a phrase. The error you're receiving sounds like you're applying it on a single term (term~10) instead of using a phrase ("term term"~10).

To do a proximity search use the tilde, "~", symbol at the end of a Phrase.

Upvotes: 8

Wyatt Barnett
Wyatt Barnett

Reputation: 15673

The only differences between Lucene.NET and classic java lucene of the same version should be internal, not external -- operational goal is to have a very compatible project, especially on the input (queries) and output (index files) side. So it should work however it works for java lucene. If it don't, it is a bug.

Upvotes: 1

Related Questions