【问题标题】:python + elasticsearch distinct field value query returning empty resultpython + elasticsearch不同字段值查询返回空结果
【发布时间】:2021-09-18 21:47:40
【问题描述】:

我对弹性搜索完全陌生。我有数据

{"username": "tom",
 "dept" : "SE",
 "location": "NY"
}

{"username": "john",
 "dept" : "SE",
 "location": "MA"
}

{"username": "tom",
 "dept" : "DQ",
 "location": "NY"
}

{"username": "mary",
 "dept" : "TY",
 "location": "TA"
}

我想让 elasticsearch 查询等效于

select distinct username from my_index

这会给我结果:

["tom", "john", "mary"]

我试过这些答案ElasticSearch - Return Unique Values

并进行了查询

query = {
            "size": 0,
            "aggs": {
                "unique_username": {
                    "terms": {
                        "field": "username.keyword",
                        "size": 200
                    }
                }
            }
        }
es.search(index="my_index", body=query)

返回

{'took': 64, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 3200, 'max_score': 0.0, 'hits': []}, 'aggregations': {'unique_username': {'buckets': []}}}

在查询的帮助下,根据这些答案,我期待唯一的用户名在存储桶列表中有计数,但存储桶似乎是空列表

{'buckets': []}

我做错了什么?

还有当我在做https://localhost:9200/my_index/_search?pretty=true&size=5

我得到结果

{

  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3200,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index_9y9f2b4e-5t90-44a2-b444-t7537fr6656b",
        "_type": "my_table",
        "_id": "1",
        "_score": 1,
        "_source": {
          "username": "tom",
          "dept": "SE",
          "location": "NY"
        }
      },
      {
        "_index": "my_index_9y9f2b4e-5t90-44a2-b444-t7537fr6656b",
        "_type": "my_table",
        "_id": "2",
        "_score": 1,
        "_source": {
          "username": "john",
          "dept": "SE",
          "location": "MA"
        }
      },
      {
        "_index": "my_index_9y9f2b4e-5t90-44a2-b444-t7537fr6656b",
        "_type": "my_table",
        "_id": "3",
        "_score": 1,
        "_source": {
          "username": "tom",
          "dept": "DQ",
          "location": "NY"
        }
      },
      {
        "_index": "my_index_9y9f2b4e-5t90-44a2-b444-t7537fr6656b",
        "_type": "my_table",
        "_id": "4",
        "_score": 1,
        "_source": {
          "username": "mary",
          "dept": "TY",
          "location": "TA"
        }
      }
    ]
  }
}

提前致谢!

【问题讨论】:

    标签: python python-3.x elasticsearch lucene


    【解决方案1】:

    成功了

    query = {
                "size": 0,
                "aggs": {
                    "unique_username": {
                        "terms": {
                            "field": "username.raw", # previously username.keyword
                            "size": 200
                        }
                    }
                }
            }
    es.search(index="my_index", body=query)
    

    这是我的愚蠢错误,只需将 username.keyword 更改为 username.raw 即可。因为raw 链接到该字段而不是keyword

    谢谢

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2016-02-02
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      相关资源
      最近更新 更多