如何在 QueryParser 中合并多个字段? [英] How to incorporate multiple fields in QueryParser?

查看:21
本文介绍了如何在 QueryParser 中合并多个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim qp1 As New QueryParser("filename", New StandardAnalyzer())
Dim qp2 As New QueryParser("filetext", New StandardAnalyzer())
.
.

我正在使用Lucene.Net"库并有以下问题.

I am using the 'Lucene.Net' library and have the following question.

不是创建两个单独的 QueryParser 对象并使用它们来获取两个 Hits 对象,是否可以使用单个 QueryParser 对象对两个字段执行搜索,以便我只有一个 Hits 对象可以为我提供每个文档的总分?

Instead of creating two separate QueryParser objects and using them to obtain two Hits objects, is it possible perform a search on both fields using a single QueryParser object, so that I have only one Hits object which gives me the overall score of each Document?

推荐答案

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

There are 3 ways to do this.

第一种方式是手动构造查询,这就是QueryParser在内部做的.这是最有效的方法,这意味着如果您想阻止访问 QueryParser 的一些更奇特的功能,则不必解析用户输入:

The first way is to construct a query manually, this is what QueryParser is 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:

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

BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("filename", "<text>"));
Query query2 = new TermQuery(new Term("filetext", "<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);

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

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.

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

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

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

最后一种方式是使用QueryParser的特殊语法看这里.

The final way is to use the special syntax of QueryParser see 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 = "filename:" + text + " OR filetext:" + text;

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

您的另一种选择是在索引名为文件名和文本的内容时创建新字段,您可以将文件名和文件文本的内容放入其中,然后您只需搜索一个字段.

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

这篇关于如何在 QueryParser 中合并多个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆