首页 > 解决方案 > 在弹性搜索中查找不同的值

问题描述

弹性搜索 7.10.0

动态映射:

{
    "mappings": {
        "dynamic_templates": [{
                "integers": {
                    "match_mapping_type": "long",
                    "mapping": {
                        "type": "integer"
                    }
                }
            },
            {
                "strings": {
                    "match_mapping_type": "string",
                    "mapping": {
                        "type": "text",
                        "fields": {
                          "raw": {
                            "type":  "keyword"
                          }
                        }
                    }
                }
            }
        ]
    }
}

Kibana 显示了以下索引映射:

{
"mappings": {
"_doc": {
  "dynamic_templates": [
    {
      "integers": {
        "match_mapping_type": "long",
        "mapping": {
          "type": "integer"
        }
      }
    },
    {
      "strings": {
        "match_mapping_type": "string",
        "mapping": {
          "fields": {
            "raw": {
              "type": "keyword"
            }
          },
          "type": "text"
        }
      }
    }
  ],
  "properties": {
     ....filtered out other properties....
     "Registry": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    },
     ....filtered out other properties....
  }
}

} }

GET /iptree_index_base/_search?filter_path=hits.total.value,took,hits.hits._source.Registry
{
  "aggs": {
    "values": {
      "terms": { "field": "Registry.raw" } 
    }
  },
  "sort" : [
      {"Registry.raw" : {"order" : "asc"}}
   ]
}

结果:

{
  "took" : 8,
  "hits" : {
    "total" : {
      "value" : 19
    },
    "hits" : [
      {
        "_source" : {
          "Registry" : "AFRINIC"
        }
      },
      {
        "_source" : {
          "Registry" : "AFRINIC"
        }
      },
      {
        "_source" : {
          "Registry" : "ARIN"
        }
      },
      {
        "_source" : {
          "Registry" : "ARIN"
        }
      },
      ..Rest of duplicate results filtered out
    ]
  }
}

期望的结果:

{
  "took" : 8,
  "hits" : {
    "total" : {
      "value" : 2
    },
    "hits" : [
      {
        "_source" : {
          "Registry" : "AFRINIC"
        }
      },
      {
        "_source" : {
          "Registry" : "ARIN"
        }
      }
    ]
  }
}

Registry.raw 是一个关键字。我错过了什么?

标签: elasticsearchkibana-7

解决方案


您对命中不感兴趣,但对聚合存储桶感兴趣。所以你正在寻找的查询是这个:

GET /iptree_index_base/_search?filter_path=hits.total.value,took,aggregations.values.buckets.key
{
  "size": 0,
  "aggs": {
    "values": {
      "terms": {
        "field": "Registry.raw",
        "order": {
          "_key": "asc"
        }
      }
    }
  }
}

推荐阅读