Java 如何在 Lucene QueryParser 中指定两个字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2005084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:23:35  来源:igfitidea点击:

How to specify two Fields in Lucene QueryParser?

javaparsinglucenelucene.netinformation-retrieval

提问by Tyzak

I read How to incorporate multiple fields in QueryParser?but i didn't get it.

我阅读了如何在 QueryParser 中合并多个字段?但我没有得到它。

At the moment i have a very strange construction like:

目前我有一个非常奇怪的结构,例如:

parser = New QueryParser("bodytext", analyzer)
parser2 = New QueryParser("title", analyzer)
query = parser.Parse(strSuchbegriff)
query2 = parser.Parse(strSuchbegriff)

What can i do for something like:

我可以为以下事情做些什么:

parser = New QuerParser ("bodytext" , "title",analyzer)
query =parser.Parse(strSuchbegriff) 

so the Parser looks for the searching word in the field "bodytext" an in the field "title".

所以解析器在“正文”字段和“标题”字段中查找搜索词。

采纳答案by Sam Doshi

There are 3 ways to do this.

有 3 种方法可以做到这一点。

The first way is to construct a query manually, this is what QueryParseris doing internally. This is the most powerful way to do it, and means that you don't have to parse the user input if you want to prevent access to some of the more exotic features of QueryParser:

第一种方法是手动构造查询,这是QueryParser内部所做的。这是执行此操作的最强大方法,这意味着如果您想阻止访问以下一些更奇特的功能,则不必解析用户输入QueryParser

IndexReader reader = IndexReader.Open("<lucene dir>");
Searcher searcher = new IndexSearcher(reader);

BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("bodytext", "<text>"));
Query query2 = new TermQuery(new Term("title", "<text>"));
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
// Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD
// for AND queries
Hits hits = searcher.Search(booleanQuery);

The second way is to use MultiFieldQueryParser, this behaves like QueryParser, allowing access to all the power that it has, except that it will search over multiple fields.

第二种方法是使用MultiFieldQueryParser,它的行为类似于QueryParser,允许访问它所拥有的所有功能,除了它将搜索多个字段。

IndexReader reader = IndexReader.Open("<lucene dir>");
Searcher searcher = new IndexSearcher(reader);

Analyzer analyzer = new StandardAnalyzer();
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                                        new string[] {"bodytext", "title"},
                                        analyzer);

Hits hits = searcher.Search(queryParser.parse("<text>"));

The final way is to use the special syntax of QueryParsersee here.

最后一种方法是使用QueryParsersee here的特殊语法。

IndexReader reader = IndexReader.Open("<lucene dir>");
Searcher searcher = new IndexSearcher(reader);    

Analyzer analyzer = new StandardAnalyzer();
QueryParser queryParser = new QueryParser("<default field>", analyzer);
// <default field> is the field that QueryParser will search if you don't 
// prefix it with a field.
string special = "bodytext:" + text + " OR title:" + text;

Hits hits = searcher.Search(queryParser.parse(special));

Your other option is to create new field when you index your content called bodytextandtitle, into which you can place the contents of bothbodytext and title, then you only have to search one field.

您的另一种选择是创建新的领域,当你索引你的内容称为bodytextandtitle,可以在其中放置的内容bodyText的和标题,那么你只需要搜索一个领域。