如何在lucene.net中使用特殊字符索引和搜索短语查询?

时间:2013-07-12 07:49:53

标签: c# lucene lucene.net

我尝试使用phrasequery搜索多个单词和特殊字符,例如“Engineering& Construction”,并添加到布尔查询中,但它没有得到任何结果。我正在索引查询的方式是

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

搜索:

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

booleanQuery包含{+ Industry:“Engineering& Construction”},即使它没有得到理想的结果。

2 个答案:

答案 0 :(得分:1)

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

生成单个词Engineering & Construction,但索引将依次包含两个词engineeringconstruction(分析符将删除&) 。像这样手动构造一个短语查询需要你理解标记,并分别添加每个术语,如:

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

当然,更简单的方法是使用查询解析器;

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

答案 1 :(得分:1)

索引:

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

搜索:

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

这对我的标准很有用。它会搜索具有特殊字符的确切短语。