Elasticsearch - Show index-wide count for each returned result based from a given term

和自甴很熟 提交于 2019-12-11 08:46:01

问题


Firstly i apologise if the terminology i use is incorrect as i am learning elasticsearch day by day and maybe use incorrect phrases.

After spending several days trying to figure this out and pulling my hair out i seem to be hitting brick walls every-time.

I am trying to get elasticsearch to provide a document count for each returned result, I will provide an example below..


{
  "suggest": {
    "text": "aberdeen",
    "city": {
      "completion": {
        "field": "city_suggest",
        "size": "2"
      }
    },
    "street": {
      "completion": {
        "field": "street_suggest",
        "size": "2"
      }
    }
  },
  "size": 0,
  "aggs": {
    "meta": {
      "filter": {
        "term": {
          "city.raw": "aberdeen"
        }
      },
      "aggs": {
        "name": {
          "terms": {
            "field": "city.raw"
          }
        }
      }
    }
  }
}

The above query returns the following results:

{
  "took": 37,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1870535,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "meta": {
      "doc_count": 119196,
      "name": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "Aberdeen",
            "doc_count": 119196
          }
        ]
      }
    }
  },
  "suggest": {
    "city": [
      {
        "text": "Aberdeen",
        "offset": 0,
        "length": 8,
        "options": [
          {
            "text": "Aberdeen",
            "score": 100
          }
        ]
      }
    ],
    "street": [
      {
        "text": "Aberdeen",
        "offset": 0,
        "length": 8,
        "options": [
          {
            "text": "Davidson House, Aberdeen, AB15",
            "score": 80
          },
          {
            "text": "Bruce House, Aberdeen, AB15",
            "score": 80
          }
        ]
      }
    ]
  }
}

The result i am trying to achieve is to have an overall document count of each returned result so for example, The returned street address of "Davidson House, Aberdeen, AB15" would say how many documents in the index match this given address and this would be repeated for each result and the same for the city in a similar way to how the aggregated city currently shows the overall count.

  {
    "key": "Aberdeen",
    "doc_count": 119196
  }

Here is an example of something similar in production


The problem i believe i have faced with aggregations is i do not know the values that are going to be returned otherwise i could predefine them with aggregations like i did the city thus requesting the overall count of each given result that way.

To help give an overall example of how i pictured the results to be i will show how i pictured that possible working results to be like:

"suggest": {
    "city": [
      {
        "text": "Aberdeen",
        "offset": 0,
        "length": 8,
        "options": [
          {
            "text": "Aberdeen",
            "score": 100,
            "total_addresses": 196152
          }
        ]
      }
    ],
    "street": [
      {
        "text": "Aberdeen",
        "offset": 0,
        "length": 8,
        "options": [
          {
            "text": "Davidson House, Aberdeen, AB15",
            "score": 80,
            "total_addresses": 158
          },
          {
            "text": "Bruce House, Aberdeen, AB15",
            "score": 80,
            "total_addresses": 30
          }
        ]
      }
    ]
  }

En terms of the elasticsearch version i am using, I have two dev servers running elasticsearch 2.3 and 5.5 to see if the newer version of elasticsearch would make a difference and unfortunately i came up short so i have been using 2.3 in favour of 5.5

Any help or advice would be greatly appreciated, Thanks all.


回答1:


you need to divide your query in two. First use the suggest API to gather suggestions, then run the aggregation on the result. The drawback of this solution would be, that you have a crazy fast suggestion (less than a millisecond, if you're lucky), against a longer running aggregation. If thats ok for you, this might be a good approach.

Another idea might be to have an own suggestion index with preaggregated data, that contains such a count - this index gets recreated regurlarly in the background.



来源:https://stackoverflow.com/questions/45533124/elasticsearch-show-index-wide-count-for-each-returned-result-based-from-a-give

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!