ElasticSearch term aggregation

后端 未结 2 978
春和景丽
春和景丽 2021-01-12 06:24

I\'m trying to perform a term aggregation using elastic search for the data below with following query, the output breaks the names into tokens (see output below). So I trie

2条回答
  •  逝去的感伤
    2021-01-12 07:17

    One solution that would work is to set the field to not_analyzed (Read more about it in the docs for attribute "index").

    This solution will not analyze the input at all, depending on your requirements you might wish to set a custom analyzer, e.g. to not split the words, but lowercase them, to get case insensitive results.

    curl -XDELETE localhost:9200/temp
    curl -XPUT localhost:9200/temp -d '
    {
      "mappings": {
        "example": {
          "properties": {
            "os_name": {
              "type": "string",
              "index" : "not_analyzed"
            },
            "os_version": {
              "type": "long"
            },
            "title": {
              "type": "string"
            }
          }
        }
      }
    }'
    
    curl -XPUT localhost:9200/temp/example/1 -d '
    {
        "title": "system3",
        "os_name": "Fedora Core",
        "os_version": 18
    }'
    
    curl -XPUT localhost:9200/temp/example/2 -d '
    {
        "title": "system1",
        "os_name": "Fedora Core",
        "os_version": 20
    }'
    
    curl -XPUT localhost:9200/temp/example/3 -d '
    {
        "title": "backup",
        "os_name": "Yellow Dog",
        "os_version": 6
    }'
    
    curl -XGET localhost:9200/temp/example/_search?pretty=true -d '
    {
      "size": 0,
      "aggs": {
         "OS": {
           "terms": {
               "field": "os_name"
           }
         }
      }
    }'
    

    Output:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 3,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "OS" : {
          "buckets" : [ {
            "key" : "Fedora Core",
            "doc_count" : 2
          }, {
            "key" : "Yellow Dog",
            "doc_count" : 1
          } ]
        }
      }
    }
    

提交回复
热议问题