如何使用 Lucene 中的 MultiFieldQueryParser?

发布于 2024-12-26 18:43:53 字数 838 浏览 2 评论 0原文

我正在使用 Version.Lucene_29。使用普通的字符串查询方法,我可以执行以下操作:

Directory directory = new FSDirectory(...);
//Start Lucene retrieval.
IndexSearcher iSearch = new IndexSearcher(directory, true);
Analyzer analyzer = new WhitespaceAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_29, "content", analyzer);
String str = 'filename:testfile.txt AND filetext:"Singapore food"'
Query query = parser.parse(str);
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;

How do i fire a query using MultiFieldQueryParser in Lucene like the string query method?

MultiFieldQueryParser multiParser = new MultiFieldQueryParser(
    Version.LUCENE_29, new String[] {"content", "ne"}, analyzer);
str = ???
Query = ????
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;

I am using Version.Lucene_29. Using the normal string query method i could do the following:

Directory directory = new FSDirectory(...);
//Start Lucene retrieval.
IndexSearcher iSearch = new IndexSearcher(directory, true);
Analyzer analyzer = new WhitespaceAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_29, "content", analyzer);
String str = 'filename:testfile.txt AND filetext:"Singapore food"'
Query query = parser.parse(str);
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;

How do i fire a query using MultiFieldQueryParser in Lucene similar to the string query method?

MultiFieldQueryParser multiParser = new MultiFieldQueryParser(
    Version.LUCENE_29, new String[] {"content", "ne"}, analyzer);
str = ???
Query = ????
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2025-01-02 18:43:53

MultiFieldQueryParser 允许您使用同一分析器在多个字段中搜索“WORD”。

例如,

 Query query = MultiFieldQueryParser.parse("development",
        new String[]{"title", "subject"},
        new SimpleAnalyzer());

它将在字段:“标题”和字段:“主题”中查找单词发展

MultiFieldQueryParser allows you to search for a "WORD" in more then one Fileds with same Analyzer.

e.g.

 Query query = MultiFieldQueryParser.parse("development",
        new String[]{"title", "subject"},
        new SimpleAnalyzer());

it will look for word development in Field : "title" and Field : "subject"

蹲墙角沉默 2025-01-02 18:43:53

MultiFieldQueryParser 是一个 QueryParser,在本例中,MultiFieldQueryParser 将两个查询创建到 BooleanClause 中。因此它还支持文件名:testfile.txt 和文件文本:“新加坡食品”

MultiFieldQueryParser is-a QueryParser, MultiFieldQueryParser creates the two Queries into a BooleanClause in this case. So it also supports filename:testfile.txt AND filetext:"Singapore food".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文