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

问题描述

查询在下面

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

如果filtername,我需要从match_all test

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

我收到错误消息“ [过滤器]中START_ARRAY的未知密钥。”

解决方法

您需要将查询包装在bool查询中,尝试以下搜索查询:

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

更新1:

根据@Nons提到的评论

搜索查询:

条款查询返回的文档中,提供的文档中包含确切的条款 字段。

{
  "from":0,"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"
            }
          ]
        }
      }
    ]

您甚至可以使用匹配查询来分析提供的文本 匹配之前。

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

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

请在下面找到插件的用法示例(条件和汇总的组合)。

enter image description here