EleasticSearch6.0 嵌套查询

1,新增索引
PUT /earth_index

{
  "mappings": {
    "earthblog": {
      "properties": {
        "title":{ "type":"string"},
        "body":{ "type":"string"},
        "comments": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string"  },
            "comment": { "type": "string"  },
            "age":     { "type": "short"   },
            "date":    { "type": "date"    }
          }
        }
      }
    }
  }
}

2,新建文档
PUT /earth_index/earthblog/1

{
  "title": "强大的搜索引擎",
  "body":  "你好这是我的一条案例...",
  "comments": [ 
    {
      "name":    "小白",
      "comment": "very goods",
      "age":     23,
      "date":    "2020-04-29"
    },
    {
      "name":    "小黑",
      "comment": "More like this please",
      "age":     18,
      "date":    "2020-04-29"
    }
  ]
}

3,查询出姓名为小白并且年龄23的数据
3.1 非嵌套查询
GET /earth_index/earthblog/_search

{
  "query": {
    "bool": {
      "must": [
        { "match": { "comments.name": "小白" }},
        { "match": { "comments.age":  23 }} 
      ]
    }
  }
}

3.2 嵌套查询
GET /earth_index/earthblog/_search

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "comments.name": "小白"
              }
            },
            {
              "match": {
                "comments.age": 23
              }
            }
          ]
        }
      }
    }
  }
}
``````json
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "comments.name": "小白"
              }
            },
            {
              "match": {
                "comments.age": 23
              }
            }
          ]
        }
      }
    }
  }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!