【发布时间】:2020-08-09 09:06:28
【问题描述】:
尝试为字段 ABC 获取匹配值 X1 或 Y1 的文档。尝试了 must 或 should 查询,但没有得到预期的结果。有人可以建议我应该尝试什么样的查询吗?使用HighLevelRestClient。
{
"bool" : {
"must" : [
{
"term" : {
"ABC" : {
"value" : "X1",
"boost" : 1.0
}
}
},
{
"term" : {
"ABC" : {
"value" : "Y1",
"boost" : 1.0
}
}
}
]
}
}
或
{
"bool" : {
"should" : [
{
"term" : {
"ABC" : {
"value" : "X1",
"boost" : 1.0
}
}
},
{
"term" : {
"ABC" : {
"value" : "Y1",
"boost" : 1.0
}
}
}
]
}
}
映射
{
"mappings": {
"properties": {
"ABC": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 2
}
}
},
mustNot 条件工作正常。如果我只是反转条件并忽略字段值,那么我会得到结果。
X1 和 Y1 是精确的字段值(想想枚举)
BoolQueryBuilder x = QueryBuilders.boolQuery();
for (SomeEnum enum : enums) {
x.should(QueryBuilders.termQuery("ABC",enum.name());
}
仍然查询返回所有文档。这应该已将文档过滤为匹配值
示例文档
{
"_index": "some_index",
"_type": "_doc",
"_id": "uyeuyeuryweoyqweo",
"_score": 1.0,
"_source": {
"A": true
"ABC": "X1"
"WS": "E"
}
},
{
"_index" : "some_index",
"_type" : "_doc",
"_id" : "uyeuyeuryweoyqweo1",
"_score" : 1.0,
"_source" : {
"A" : true,
"ABC" : "Y1",
"WS" : "MMM"
}
}
【问题讨论】:
-
您能否展示您的映射、示例文档和预期输出?
-
添加了更多上下文
标签: elasticsearch elasticsearch-rest-client