elasticsearch获得特定领域

时间:2018-01-10 06:16:10

标签: elasticsearch

我正在尝试从以下索引数据中搜索数据

   PUT /nested_test100/t/1
  {
    "title": "Nest eggs1",
    "body":  "Making your money work...",
    "tags":  [ "cash1", "shares1" ],
    "comments": [ 
      {
        "name":    "John Smith1",
        "comment": "Great article1",
        "age":     28,
        "stars":   1,
        "date":    "2014-09-01"
      },
      {
        "name":    "Alice White1",
        "comment": "More like this please1",
        "age":     31,
        "stars":   1,
        "date":    "2014-10-22"
      }
    ]
  }

  PUT /nested_test100/t/2
  {
    "title": "Nest eggs2",
    "body":  "Making your money work...",
    "tags":  [ "cash", "shares" ],
    "comments": [ 
      {
        "name":    "John Smith2",
        "comment": "Great article2",
        "age":     30,
        "stars":   2,
        "date":    "2014-09-01"
      },
      {
        "name":    "Alice White2",
        "comment": "More like this please2",
        "age":     31,
        "stars":   2,
        "date":    "2014-10-22"
      }
    ]
  }

  PUT /nested_test100/t/3
  {
    "title": "Nest eggs3",
    "body":  "Making your money work...",
    "tags":  [ "cash3", "shares3" ],
    "comments": [ 
      {
        "name":    "John Smith3",
        "comment": "Great article3",
        "age":     28,
        "stars":   3,
        "date":    "2014-09-01"
      },
      {
        "name":    "Alice White3",
        "comment": "More like this please3",
        "age":     30,
        "stars":   3,
        "date":    "2014-10-22"
      }
    ]
  }

  GET /nested_test100/t/_search

我想要的只是获得标题,身体标签,只有年龄= 28的评论。

我应该如何编写查询dsl。

我写的是

POST /nested_test100/t/_search
{
  "fields" : ["title","comments.age","body","tags"],
  "query"  : {
             "term" : { "comments.age" : "28" }
           }
}

它给我这样的数据

     "hits": [
     {
        "_index": "nested_test100",
        "_type": "t",
        "_id": "1",
        "_score": 1,
        "fields": {
           "comments.age": [
              28,
              31
           ],
           "title": [
              "Nest eggs1"
           ],
           "body": [
              "Making your money work..."
           ],
           "tags": [
              "cash1",
              "shares1"
           ]
        }
     },
     {
        "_index": "nested_test100",
        "_type": "t",
        "_id": "3",
        "_score": 1,
        "fields": {
           "comments.age": [
              28,
              30
           ],
           "title": [
              "Nest eggs3"
           ],
           "body": [
              "Making your money work..."
           ],
           "tags": [
              "cash3",
              "shares3"
           ]
        }
     }
  ]

但我不想要年龄超过28岁的评论。

我正在使用elasticsearch版本1.7

1 个答案:

答案 0 :(得分:0)

请使用inner_hits将嵌套的内部对象包含为搜索匹配的内部匹配。

搜索请求如下:

POST test/_search
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": {"comments.age" : 28}
      },
      "inner_hits": {} 
    }
  }
}

P.S:您需要将注释映射为嵌套字段(如Val所述)