elasticsearch - Elasticsearch:搜索嵌套参数时出现问题

标签 elasticsearch

PUT test
{  
   "mappings":{  
      "folks":{  
         "properties":{  
            "works_at": {
              "type": "nested"
            }
         }
      }
   }
}


PUT /test/folks/1
{
  "type": "lawyer",
  "works_at": [
    { "location":"New York"},
    { "location":"Boston"}
  ]
}

PUT /test/folks/2
{
  "type": "lawyer",
  "works_at": [
       {"location":"Chicago"},
       {"location":"Boston"}
     ] 

}

PUT /test/folks/3
{
  "type": "writer",
  "works_at": [
       {"location":"San Francisco"},
       {"location":"Boston"}
     ] 
}

该搜索带来零结果:
GET /test/folks/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "works_at.location": "Boston"
          }
        },
        {
          "match": {
            "type": "lawyer"
          }
        }
      ]
    }
  }
}

如果将位置部分从搜索中删除:
GET /test/folks/_search
{
  "query": {
    "bool": {
      "must": [

        {
          "match": {
            "type": "lawyer"
          }
        }
      ]
    }
  }
}

它将带来正确的结果:
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "folks",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "type": "lawyer",
          "works_at": [
            {
              "location": "Chicago"
            },
            {
              "location": "Boston"
            }
          ]
        }
      },
      {
        "_index": "test",
        "_type": "folks",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "type": "lawyer",
          "works_at": [
            {
              "location": "New York"
            },
            {
              "location": "Boston"
            }
          ]
        }
      }
    ]
  }
}

我的多重搜索查询中缺少什么?

最佳答案

要搜索嵌套对象,您需要使用nested query

由于locationworks_at的属性嵌套对象,因此您必须按以下方式修改查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "works_at",
            "query": {
              "match": {
                "works_at.location": "Boston"
              }
            }
          }
        },
        {
          "match": {
            "type": "lawyer"
          }
        }
      ]
    }
  }
}

关于elasticsearch - Elasticsearch:搜索嵌套参数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152527/

相关文章:

elasticsearch - 如何使用elasticsearch查询对象中的值

elasticsearch - ElasticSearch日期字段统计

elasticsearch - Elasticsearch何时确定文档的索引?

Elasticsearch Map 对 not_analyzed 文档不区分大小写

java - Elasticsearch 失败,错误为 "Failed to execute phase [query_fetch], all shards failed"

elasticsearch - 在字段名称中使用通配符的Elasticsearch范围查询

java - 如何在elasticsearch中从父id获取子列表

elasticsearch - Elasticsearch/Lucene首字母缩写词搜索

elasticsearch - 安装Marvel插件Elasticsearch时出现问题

elasticsearch - 如何在Elasticsearch中搜索字段数组