2

试图获取匹配值X1Y1字段的文档ABC。尝试了两个mustshould查询,但没有得到预期的结果。有人可以建议我应该尝试什么样的查询吗?使用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"
  }
}


4

1 回答 1

2

由于您没有提供映射,可能的原因是搜索时间标记与索引标记不匹配。

由于您使用的是文档term中提到的未分析的查询

返回在提供的字段中包含确切术语的文档。

这意味着您在索引中的文档必须包含确切的标记,X1并且Y1如果这些字段是text字段并且您没有定义任何分析器,而不是弹性搜索使用标记的standard分析器lowercases,因此在索引中x1并且y1将被存储并且没有任何匹配。

编辑:正如怀疑的那样,问题是由于termtext字段上使用的查询,下面的查询会给出预期的结果

{
  "bool" : {
    "should" : [
      {
        "term" : {
          "ABC.keyword" : {
            "value" : "X1",
            "boost" : 1.0
          }
        }
      },
      {
        "term" : {
          "ABC.keyword" : {
            "value" : "Y1",
            "boost" : 1.0
          }
        }
      }
    ]
  }
}
于 2020-08-09T09:12:18.097 回答