简体   繁体   中英

how to index and search phrase query with special characters in lucene.net?

I tried to search multiple words and special characters like "Engineering & Construction" using phrasequery and added in to boolean query but its not getting any result.The way i'm indexing the query is

doc.Add(new Field("Industry","Engineering & Construction", Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));

For Searching:

var booleanQuery = new BooleanQuery();
PhraseQuery phrasequery = new PhraseQuery();
phrasequery.Add(new Term("Industry","Engineering & Construction"));
booleanQuery.Add(phraseQuery, BooleanClause.Occur.MUST);

the booleanQuery contains {+Industry:"Engineering & Construction"} even though its not getting desired result.

This

phrasequery.Add(new Term("Industry","Engineering & Construction"));

Produces a single term, Engineering & Construction , but the index will have two terms, engineering and construction , in sequence (the & will be removed by the analyzer). Constructing a phrasequery manually like this requires you to understand the tokens, and add each term separately, like:

phrasequery.Add(new Term("Industry","engineering"));
phrasequery.Add(new Term("Industry","construction"));

Of course, the easier way is to use a query parser;

Query phraseQuery = queryparser.parse("Industry:Engineering & Construction");
booleanquery.add(phraseQuery);

For Indexing :

doc.Add(new Field("Industry","Engineering & Construction", Field.Store.YES, Field.Index.NOT_ANALYZED));

For Searching :

TermQuery query = new TermQuery(new Term("Industry", "Engineering & Construction"));
booleanQuery.Add(query, BooleanClause.Occur.MUST);

It was helpful for my criteria.It searches the exact phrase with special characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM