此elasticsearch或查询有哪些错误?

tpgth1q7  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(271)

我试图在一个查询中合并两个子句,但我看到一些奇怪的行为,正在寻求指导
给定一个索引“index1”和两个文档-

{"column1": "A"} with _id=1
{"column1": "B"} with _id=2

使用以下正文运行post index1/\u搜索时-

{"query": {
    "bool": {
        "minimum_should_match": 1,
        "should": [
            {
                "bool": {
                    "must": [
                        {"term": {"_id": "1"}},
                        {"term": {"column1": "A"}}
                    ]
                }
            },
            {
                "bool": {
                    "must": [
                        {"term": {"_id": "2"}},
                        {"term": {"column1": "B"}}
                    ]
                }
            }
        ]
    }
}}

我没有结果。我所期待的是这两份文件都会被退回。如果删除其中一个column1术语匹配项,我将获得另一个文档。如果我删除两个column1术语匹配项,我会同时得到这两个文档吗?
我做错什么了?

6ie5vjzr

6ie5vjzr1#

正如@val已经建议的,您应该使用 column1.keword ,如果您使用的是 column1 搜索查询:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "_id": "1"
                }
              },
              {
                "term": {
                  "column1.keyword": "A"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "_id": "2"
                }
              },
              {
                "term": {
                  "column1.keyword": "B"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "fd_cb",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.6931472,
        "_source": {
          "column1": "A"
        }
      },
      {
        "_index": "fd_cb",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.6931472,
        "_source": {
          "column1": "B"
        }
      }
    ]

如果已经为定义了显式Map column1 ,如下图所示:

{
  "mappings": {
    "properties": {
      "column1": {
        "type": "keyword"
      }
    }
  }
}

那么您的问题中提到的搜索查询就可以很好地工作了。

相关问题