java - 如何在 Apache Lucene 中将 PhraseQuery 与 RangeQuery 结合使用?
问题描述
基本上,我希望能够使用以下查询来查询索引"phrase query" AND date:[20180101 TO 20181231]
。我尝试使用MultiFieldQueryParser
,但出现以下错误:
Exception in thread "main" java.lang.IllegalStateException: field "date" was indexed without position data; cannot run PhraseQuery
所以我使用如下代码构造了查询:
final Query phraseQ = new QueryParser("text", Analyzer.CUSTOM)
.parse(query);
final Query rangeQ = new QueryParser("date", Analyzer.CUSTOM)
.parse(dateRange);
final Query q = new BooleanQuery.Builder()
.add(phraseQ, BooleanClause.Occur.MUST)
.add(rangeQ, BooleanClause.Occur.MUST)
.build();
其中查询是"phrase query"~1
和 dateRange 是date:[20180101 TO 20181231]
。我仍然得到以下异常
Exception in thread "main" java.lang.IllegalStateException: field "date" was indexed without position data; cannot run PhraseQuery (phrase=date:"phrase query"~1)
虽然我不使用短语查询日期字段。
短语查询是否可以与范围查询相结合?
解决方案
MultiFieldQueryParser 旨在搜索在查询中未指定字段时给出的所有字段列表。所以这:
MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {"text","date"}, new StandardAnalyzer());
Query query = parser.parse("\"phrase query\" AND date:[20180101 TO 20181231]");
生成如下所示的查询:
+(text:"phrase query" date:"phrase query") +date:[20180101 TO 20181231]
因此问题。如果您仅将其替换为标准QueryParser
,并将“文本”作为默认字段,它应该会产生您想要的内容。
但是,就您提供的代码而言,它看起来不错。尝试dateRange
在解析之前打印它的内容,确保它是你认为的那样。
推荐阅读
- asp.net-core - 页面和操作明智的授权 Asp.Net Core Razor 页面
- bitmap - PPM 格式是行业标准吗?
- html - 为什么前缀 data-* 在 Angular 9 中不起作用?
- javascript - JavaScript:在带有链接的按钮上播放音频
- java - 在 Docker Swarm 中运行集群时暴露 Nifi UI
- javascript - 对地图函数中的第一个元素做不同的事情
- java - 比较路径 (Windows/Linux)
- python - 使用 Click 的清除功能
- flutter - 如何在 Flutter 的 TextField 上格式化输入货币(从右到左)?
- spring - 使用 applicationContext 提取 Bean 后如何在 Bean 上调用 initMethod