Elasticsearch - отдельные документы для запроса с функцией function_score - PullRequest
0 голосов
/ 25 мая 2019

У меня есть индекс в Elasticsearch.Документы в нем имеют повторяющиеся значения полей.И в результате запроса мне нужно удалить все дубликаты и получить только различные значения.Например:

PUT localhost: 9200 / чел.

{
    "mappings" : {
        "person" : {
            "properties" : {
                "name" : { "type" : "keyword" }
            }
        }
    }
}

POST localhost: 9200 / чел. / Чел.

{
    "name": "John"
}

{
    "name": "John"
}

{
    "name": "Marry"
}

{
    "name": "Tomas"
}

Я пытаюсь удалить дубликаты с условиямиагрегирование по полю «имя», но оно не работает.

GET localhost: 9200 / person / person / _search

{
  "size": 3,
  "query": {
    "function_score": {
      "functions": [
        {
          "random_score": {
            "seed": "dasdfdLBpnM0"
          }
        }
      ]
    }
  },
  "aggs": {
    "top-names": {
      "terms": {
        "field": "name",
        "size": 3
      },
      "aggs": {
        "top_names_hits": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

Результат:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": 0.9506482,
        "hits": [
            {
                "_index": "person",
                "_type": "person",
                "_id": "H-5D8GoB8pRyckNSVUeN",
                "_score": 0.9506482,
                "_source": {
                    "name": "Tomas"
                }
            },
            {
                "_index": "person",
                "_type": "person",
                "_id": "He5D8GoB8pRyckNSPEfa",
                "_score": 0.7700638,
                "_source": {
                    "name": "John"
                }
            },
            {
                "_index": "person",
                "_type": "person",
                "_id": "HO5D8GoB8pRyckNSN0fo",
                "_score": 0.71723765,
                "_source": {
                    "name": "John"
                }
            }
        ]
    },
    "aggregations": {
        "top-names": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "John",
                    "doc_count": 2,
                    "top_names_hits": {
                        "hits": {
                            "total": 2,
                            "max_score": 0.7700638,
                            "hits": [
                                {
                                    "_index": "person",
                                    "_type": "person",
                                    "_id": "He5D8GoB8pRyckNSPEfa",
                                    "_score": 0.7700638,
                                    "_source": {
                                        "name": "John"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "Marry",
                    "doc_count": 1,
                    "top_names_hits": {
                        "hits": {
                            "total": 1,
                            "max_score": 0.66815424,
                            "hits": [
                                {
                                    "_index": "person",
                                    "_type": "person",
                                    "_id": "Iu5D8GoB8pRyckNScUdv",
                                    "_score": 0.66815424,
                                    "_source": {
                                        "name": "Marry"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "Tomas",
                    "doc_count": 1,
                    "top_names_hits": {
                        "hits": {
                            "total": 1,
                            "max_score": 0.9506482,
                            "hits": [
                                {
                                    "_index": "person",
                                    "_type": "person",
                                    "_id": "H-5D8GoB8pRyckNSVUeN",
                                    "_score": 0.9506482,
                                    "_source": {
                                        "name": "Tomas"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

Агрегация применяется к документам с именем = "Жениться", но я не понимаю, почему и как я могу применить агрегацию только к результатам запроса.

1 Ответ

0 голосов
/ 26 мая 2019

Ниже приведен более или менее план Elasticsearch Query ....

{
  "size": n, // Return the n documents based on "query" section (to frontend)

  "query": {
          //  Here is where you are supposed to mention what documents you want
          //  Any filter/bool/match query condition
          //  In your case, you haven't specified any correct condition. 
          //  So basically, it would return all the documents or documents based on size parameter. In your case it returns 3. 
  },

  "aggs":{
      //  This aggregation query would only be applied on documents 
      //  based on documents filtered/matched by the "query" section. 
      //  In your case it is applying aggregation on all documents of that index as per the comment I've mentioned in the above query section.
   }
}

Запрос агрегации:

Чтобы получить то, что вы ищете, просто воспользуйтесь ниже упрощенным запросом, который у вас был с Обобщением условий с Лучшими хитами в качестве субагрегации.

POST person/_search
{
  "size": 0,                          <------- This is to say, I don't want "query" results to be returned and that I only want below aggregation results. 
  "aggs": {
    "top-names": {
      "terms": {
        "field": "name",
        "size": 10
      },
      "aggs": {
        "top_hits_documents": {       <------- Top hits would return the actual documents
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

Указав "size": 0, в самом верху вы в основном применяете агрегирование ко всем документам, и вы не возвращаете любые результаты запроса .

Вы просто возвращаете результаты агрегации .

Ответ:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 0.0,
    "hits" : [ ]                    <------ Notice this. No query results returned
  },
  "aggregations" : {                <------ Aggregation Result starts
    "top-names" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "John",           <------- This is to say there's a value called John 
          "doc_count" : 2,          <------- John occurs in two documents.
          "top_hits_documents" : {
            "hits" : {
              "total" : 2,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "person",
                  "_type" : "person",
                  "_id" : "2",
                  "_score" : 1.0,
                  "_source" : {
                    "name" : "John"
                  }
                }
              ]
            }
          }
        },
        {
          "key" : "Marry",
          "doc_count" : 1,
          "top_hits_documents" : {
            "hits" : {
              "total" : 1,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "person",
                  "_type" : "person",
                  "_id" : "3",
                  "_score" : 1.0,
                  "_source" : {
                    "name" : "Marry"
                  }
                }
              ]
            }
          }
        },
        {
          "key" : "Thomas",
          "doc_count" : 1,
          "top_hits_documents" : {
            "hits" : {
              "total" : 1,
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "person",
                  "_type" : "person",
                  "_id" : "4",
                  "_score" : 1.0,
                  "_source" : {
                    "name" : "Thomas"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Надеюсь, это поможет!

...