ElasticSearch 2.4 - результат вложенных запросов слияния inner_hits - PullRequest
1 голос
/ 09 апреля 2020

Я использую ElasticSearch 2.4

Мне нужно получить все покупки, которые соответствуют всем запросам.

Я на самом деле использую функцию inner_hits, но она не работает, как ожидалось, потому что она только показывает совпадение текущего вложенного запроса, и проблема заключается в комбинации с запросом основного документа.

У меня есть это отображение, и ниже я создал пример с моими комментариями:

PUT /example_contact_purchases
{
  "mappings": {
    "contact": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "country": {
          "type": "string"
        },
        "purchases": {
          "type": "nested",
          "properties": {
            "uuid":{
              "type":"string"
            },
            "brand":{
              "type":"string"
            }
          }
        }
      }
    }
  }
}
POST example_contact_purchases/contact
{
  "name" : "Fran",
  "country": "ES",
  "purchases" : [
    {
      "uuid" : "23",
      "brand":"Sony"
    }, 
    {
      "uuid":"23",
      "brand":"Sony"
    }
  ]
}
POST example_contact_purchases/contact
{
  "name" : "Jhon",
  "country": "UK",
  "purchases" : [
    {
      "uuid" : "45",
      "brand": "Lenovo"
    },
    {
      "uuid":"23",
      "brand":"Sony"
    },
    {
      "uuid":"77",
      "brand":"HP"
    }
  ]
}
POST example_contact_purchases/contact
{
  "name" : "Lucas",
  "country": "ES",
  "purchases" : [
    {
      "uuid" : "45",
      "brand": "Lenovo"
    },
    {
      "uuid":"23",
      "brand":"Sony"
    },
    {
      "uuid":"77",
      "brand":"HP"
    }
  ]
}

GET example_contact_purchases/contact/_search
  {
      "query": {
          "bool": {
              "should": [
                  {"bool": {
                      "must": [
                          {
                              "query_string": {
                                  "query": "country:ES"
                              }
                          },
                          {
                              "nested": {
                                  "path": "purchases",
                                  "inner_hits":{
                                    "name":"0"
                                  },
                                  "filter": {
                                      "query": {
                                          "query_string": {
                                              "query": "(purchases.brand:Sony)"
                                          }
                                      }
                                  }
                              }
                          }
                      ]
                  }},
                  {"bool": {
                      "must": [
                          {
                              "query_string": {
                                  "query": "country:UK"
                              }
                          },
                          {
                              "nested": {
                                  "path": "purchases",
                                  "inner_hits":{
                                    "name":"1"
                                  },
                                  "filter": {
                                      "query": {
                                          "query_string": {
                                              "query": "(purchases.uuid:45)"
                                          }
                                      }
                                  }
                              }
                          }
                      ]

                  }
                  }
              ]
          }
      }
  }

Я используя простой запрос, подобный следующему:

 "(country.raw:ES AND purchases.brand:Sony) OR (country:UK AND purchases.uuid:45)" 

И результат поискового запроса:

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.5949223,
    "hits": [
      {
        "_index": "example_contact_purchases",
        "_type": "contact",
        "_id": "AXFfJJdZXthyTIlmcERM",
        "_score": 0.5949223,
        "_source": {
          "name": "Jhon",
          "country": "UK",
          "purchases": [
            {
              "uuid": "45",
              "brand": "Lenovo"
            },
            {
              "uuid": "23",
              "brand": "Sony"
            },
            {
              "uuid": "77",
              "brand": "HP"
            }
          ]
        },
        "inner_hits": {
          "0": {
            "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJJdZXthyTIlmcERM",
                  "_nested": {
                    "field": "purchases",
                    "offset": 1
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "23",
                    "brand": "Sony"
                  }
                }
              ]
            }
          },
          "1": {
            "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJJdZXthyTIlmcERM",
                  "_nested": {
                    "field": "purchases",
                    "offset": 0
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "45",
                    "brand": "Lenovo"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "example_contact_purchases",
        "_type": "contact",
        "_id": "AXFfJKBHXthyTIlmcERN",
        "_score": 0.5949223,
        "_source": {
          "name": "Lucas",
          "country": "ES",
          "purchases": [
            {
              "uuid": "45",
              "brand": "Lenovo"
            },
            {
              "uuid": "23",
              "brand": "Sony"
            },
            {
              "uuid": "77",
              "brand": "HP"
            }
          ]
        },
        "inner_hits": {
          "0": {
            "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJKBHXthyTIlmcERN",
                  "_nested": {
                    "field": "purchases",
                    "offset": 1
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "23",
                    "brand": "Sony"
                  }
                }
              ]
            }
          },
          "1": {
            "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJKBHXthyTIlmcERN",
                  "_nested": {
                    "field": "purchases",
                    "offset": 0
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "45",
                    "brand": "Lenovo"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "example_contact_purchases",
        "_type": "contact",
        "_id": "AXFfJI1SXthyTIlmcERL",
        "_score": 0.5139209,
        "_source": {
          "name": "Fran",
          "country": "ES",
          "purchases": [
            {
              "uuid": "23",
              "brand": "Sony"
            },
            {
              "uuid": "23",
              "brand": "Sony"
            }
          ]
        },
        "inner_hits": {
          "0": {
            "hits": {
              "total": 2,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJI1SXthyTIlmcERL",
                  "_nested": {
                    "field": "purchases",
                    "offset": 1
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "23",
                    "brand": "Sony"
                  }
                },
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJI1SXthyTIlmcERL",
                  "_nested": {
                    "field": "purchases",
                    "offset": 0
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "23",
                    "brand": "Sony"
                  }
                }
              ]
            }
          },
          "1": {
            "hits": {
              "total": 0,
              "max_score": null,
              "hits": []
            }
          }
        }
      }
    ]
  }
}

К сожалению, первый результат неверен:

"inner_hits": {
          "0": {
            "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJJdZXthyTIlmcERM",
                  "_nested": {
                    "field": "purchases",
                    "offset": 1
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "23",
                    "brand": "Sony"
                  }
                }
              ]
            }
          },
          "1": {
            "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                {
                  "_index": "example_contact_purchases",
                  "_type": "contact",
                  "_id": "AXFfJJdZXthyTIlmcERM",
                  "_nested": {
                    "field": "purchases",
                    "offset": 0
                  },
                  "_score": 1,
                  "_source": {
                    "uuid": "45",
                    "brand": "Lenovo"
                  }
                }
              ]
            }
          }
        }

Это должен показать покупку для Jhon UK с параметрами:

 {"uuid": "45","brand":"Lenovo"} ( inner_hits with name "1") 

Спасибо

...