Elasticsearch嵌套查询和排序

时间:2018-09-27 13:23:34

标签: elasticsearch nested

有人可以帮助我了解Elastic嵌套的含义。在文档https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_nested_sorting_examples中是一个示例,该示例未显示文档对象的外观。看起来我应该想象搜索查询中的映射。查询如下:

POST /_search
{
   "query": {
      "nested": {
         "path": "parent",
         "query": {
            "bool": {
                "must": {"range": {"parent.age": {"gte": 21}}},
                "filter": {
                    "nested": {
                        "path": "parent.child",
                        "query": {"match": {"parent.child.name": "matt"}}
                    }
                }
            }
         }
      }
   },
   "sort" : [
      {
         "parent.child.age" : {
            "mode" :  "min",
            "order" : "asc",
            "nested": {
               "path": "parent",
               "filter": {
                  "range": {"parent.age": {"gte": 21}}
               },
               "nested": {
                  "path": "parent.child",
                  "filter": {
                     "match": {"parent.child.name": "matt"}
                  }
               }
            }
         }
      }
   ]
}

有人可以编写此查询可使用的文档结构吗?

2 个答案:

答案 0 :(得分:1)

类似这样的事情。

{
    "parent": {
        "name": "Elasti Sorch",
        "age": 23,
        "child": [
           {
              "name": "Kibana Lion",
              "age": 12
           }, 
           {
              "name": "Matt",
              "age": 15
           }
        ] 
    }
}

答案 1 :(得分:1)

在弹性嵌套中表示它是一个对象数组。要将对象数组存储到弹性搜索的字段中,必须在创建索引时将字段映射到嵌套的对象。

PUT parent
       {
       "mappings": {
        "doc":{
      "properties": {
        "name":{
          "type": "text"
        },
        "age":{
          "type": "integer"
        },
        "child":{
          "type": "nested",
          "properties": {
            "name":{
              "type":"text"
            },
            "age":{
              "type":"integer"
            }
          }

        }
      }
    }
  }
}

像这样插入一个嵌套的示例文档

POST parent/doc
{
  "name":"abc",
  "age":50,
  "child":[
    {
      "name":"son1",
      "age":25
    },
    {
      "name":"adughter1",
      "age":20
    }
    ]
}