如何在弹性搜索中使用 match_all 中的过滤器

How to use filter in match_all in elastic search

查询如下

{"from" : 0, "size" : 100,"query": {"match_all": {}}})

如果 nametest

,我需要 match_all filter

我试过 {"from" : 0, "size" : 100,"query": {"match_all": {}}, "filter": [ "term": { "name": "test" }}]}

我收到错误 'Unknown key for a START_ARRAY in [filter].')

您需要将查询包装在 bool 查询中,试试这个搜索查询:

    {
  "from":0,
  "size":10,
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": [
        {
          "term": {
           "grocery_name": "elastic"
          }
        }
      ]
    }
  }
}

更新 1:

根据@Nons提到的评论

搜索查询:

Terms query return documents that contain an exact term in a provided field.

{
  "from":0,
  "size":10,
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": [
        {
          "term": {
           "parentName.keyword": "Developer"    <-- note this
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64275684",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "id": "1",
          "name": "A",
          "parentName": "Developer",
          "Data": [
            {
              "id": "455",
              "name": "Google",
              "lastUpdatedDate": "2020-09-10",
              "parent_id": "1"
            }
          ],
          "Function": [
            {
              "id": "1",
              "name": "Major"
            }
          ]
        }
      }
    ]

You can even use a match query where the provided text is analyzed before matching.

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": {
        "match": {
          "parentName": "developer"
        }
      }
    }
  }
}

我建议使用 Chrome ElasticSearch Head 插件。它允许非常容易地针对 Elastic 进行测试和 运行 搜索(功能类似于 MySql Workbech)。

请在下面找到插件的使用示例(条件和聚合的组合)。