elast cisearchネストクエリ

10639 ワード

公式文書を検索https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-request-sort.html#_neted_sorting_exampleも参照できます。http://blog.csdn.net/u012332735/article/details/62222953
PUT /testnexted/my_type/1?refresh
{
   "product": "xxx",
   "offer": [{
     "price": 45,
     "color": "blue"
   },
   {
     "price": 18,
     "color": "red"
   },
   {
     "price": 45,
     "color": "red"
   },
   {
     "price": 28,
     "color": "red"
   }
   ]
}

PUT /testnexted/my_type/2?refresh
{
   "product": "xxx56",
   "offer": [{
     "price": 23,
     "color": "blue"
   },
   {
     "price": 10,
     "color": "red"
   },
   {
     "price": 58,
     "color": "red"
   },
   {
     "price": 32,
     "color": "red"
   }
   ]
}

//defined
PUT /testnexted/
{
    "mappings": {
        "my_type": {
            "properties": {
                "offer": { "type": "nested" },
                "price": {
                    "type": "integer"
                },
                "color": {
                    "type": "keyword"
                }
            }
        }
    }
}

//search
POST /testnexted/_search?pretty


//sort
POST /testnexted/_search
{
    "sort" : [
     {
        "offer.price" : {
           "order" : "asc",
           "nested_path" : "offer",
           "nested_filter" : {
              "term" : { "offer.color" : "blue" }
           }
        }
     }
  ]

}

//         
POST /testnexted/_search
{
    "sort" : [
     {
        "offer.price" : {
           "mode" :  "avg",
           "order" : "asc",
           "nested_path" : "offer",
           "nested_filter" : {
              "term" : { "offer.color" : "blue" }
           }
        }
     }
  ]

}
結果
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "testnexted",
        "_type": "my_type",
        "_id": "2",
        "_score": null,
        "_source": {
          "product": "xxx56",
          "offer": [
            {
              "price": 23,
              "color": "blue" },
            {
              "price": 10,
              "color": "red" },
            {
              "price": 58,
              "color": "red" },
            {
              "price": 32,
              "color": "red" }
          ]
        },
        "sort": [
          23
        ]
      },
      {
        "_index": "testnexted",
        "_type": "my_type",
        "_id": "1",
        "_score": null,
        "_source": {
          "product": "xxx",
          "offer": [
            {
              "price": 45,
              "color": "blue" },
            {
              "price": 18,
              "color": "red" },
            {
              "price": 45,
              "color": "red" },
            {
              "price": 28,
              "color": "red" }
          ]
        },
        "sort": [
          45
        ]
      }
    ]
  }
}