Elastic Search - display all distinct values of an array

狂风中的少年 提交于 2019-12-11 08:36:57

问题


For a field mapped as string I have stored list of strings in the ES index, for ex:

  subject: ["Scientific Research", "Numerical Analysis", "History of Art"]

I would like to query this field and retrieve the full names of categories with their frequency count. What I tried so far with facets:

  "query":{
       "match_all": {}
   }, 
   "facets":{
       "tag":{
           "terms":{
               "field":"subject"}
             }
   }  

is not working as expected because it splits my subject fields into tokens and returns me the top most frequent stopwords. How can I get full entries ordered by counts for an analyzed field, and not only the top 10, if possible? Thanks!


回答1:


I would use a multi-field define your mapping like so -

{
   .....
        ....
            .....
            "subject": {
                "type": "multi_field",
                "store": "yes",
                "fields": {
                    "analyzed": {
                        "type": "string",
                        "analyzer": "standard"
                    },
                    "notanalyzed": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }

Then I would carry out your faceting on the notanalyzed field like so -

"query":{
      "match_all": {}
  }, 
  "facets":{
      "tag":{
          "terms":{
              "field":"subject.notanalyzed",
              "size": 50
            }
        }
  }  


来源:https://stackoverflow.com/questions/23040782/elastic-search-display-all-distinct-values-of-an-array

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