timu
timu

Reputation: 838

Lucene .NET searching

Hi i am trying to make autocomplete system using Lucene library to search over 170K records.

But there is a litle problem.

For example when i search for Candice Gra(...), it brings records like

Candice Jackson
Candice Hamilton
Candice Hayes

Bu not Candice Graham to make Lucene find Candice Graham i need to type Candice Graham exactly.

Here is the code that i'm building query.

Directory directory = FSDirectory.Open(new DirectoryInfo(context.Server.MapPath("
ISet<string> stopWordSet = new HashSet<string>(stopWords);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30, stopWordSet);

IndexReader indexReader = IndexReader.Open(directory, true);
Searcher indexSearch = new IndexSearcher(indexReader);

//Singe Field Search
var queryParser = new QueryParser(Version.LUCENE_30,
                        "Title",
                        analyzer);
string strQuery = string.Format("{0}", q);
var query = queryParser.Parse(strQuery);

If i build strQuery like this (* appended to the query)

string strQuery = string.Format("{0}*", q);

But using this way brings irrelevant records too. For example if i search Candice Gra(...) again it returns records like

Grass
Gravity
Gray (etc.)

By the way i used KeywordAnalyzer and SimpleAnalyzer but these are not worked either. Any ideas?

Upvotes: 0

Views: 912

Answers (2)

Bj&#246;rn
Bj&#246;rn

Reputation: 29381

You should escape your spaces if you want them included in the search;

var query = queryParser.Parse(QueryParser.Escape(strQuery));

Upvotes: 2

rtcardoso
rtcardoso

Reputation: 111

I think you need to put a AND keyword between these two words.

"Candice" AND "Gra"

http://lucene.apache.org/core/2_9_4/queryparsersyntax.html#AND

Upvotes: 1

Related Questions