Elasticsearch查询将多个值匹配到单个字段

ldxq2e6h  于 5个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(75)

尝试获取与字段ABC的值X1Y1匹配的文档。尝试了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
          }
        }
      }
    ]
  }
}


Map

{
  "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"
  }
}

irlmq6kh

irlmq6kh1#

由于您没有提供Map,可能的原因是搜索时间标记与索引标记不匹配。
由于您使用的是term查询,因此不会像doc中提到的那样进行分析
返回在提供的字段中包含精确术语的文档。
这意味着索引中的文档必须包含X1Y1的确切标记,如果这些字段是text字段,并且您没有定义任何分析器,则elasticsearch使用standard分析器,其中lowercases标记,因此在索引中存储x1y1,并且没有任何匹配。

编辑:如怀疑的那样,问题是由于在text字段上使用了term查询,以下查询将给予预期结果

{
  "bool" : {
    "should" : [
      {
        "term" : {
          "ABC.keyword" : {
            "value" : "X1",
            "boost" : 1.0
          }
        }
      },
      {
        "term" : {
          "ABC.keyword" : {
            "value" : "Y1",
            "boost" : 1.0
          }
        }
      }
    ]
  }
}

字符串

bakd9h0s

bakd9h0s2#

这对我很有效

{"query":
{"bool":
{"filter":["must" : [{ "match" : { "ABC" : "X1 OR Y1"}}]}}

字符串

相关问题