试图获取匹配值X1
或Y1
字段的文档ABC
。尝试了两个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"
}
}