Anonymous
Python + elasticsearch запрос значения отдельного поля, возвращающий пустой результат
Сообщение
Anonymous » 13 янв 2026, 16:55
Я совершенно новичок в эластичном поиске. У меня есть данные
Код: Выделить всё
{"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 эквивалентным
что даст мне результат:
Я попробовал эти ответы ElasticSearch — возврат уникальных значений
И сделал запрос
Код: Выделить всё
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': []}}}
с помощью запроса, согласно этим ответам, я ожидал уникальное имя пользователя с соответствующим количеством значений в списке сегментов, но список, похоже, пустой
Что я делаю не так?
Также, когда я выполняю
https://localhost:9200/my_index/_search ... rue&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"
}
}
]
}
}
Заранее спасибо!
Подробнее здесь:
https://stackoverflow.com/questions/683 ... pty-result
1768312513
Anonymous
Я совершенно новичок в эластичном поиске. У меня есть данные [code]{"username": "tom", "dept" : "SE", "location": "NY" } {"username": "john", "dept" : "SE", "location": "MA" } {"username": "tom", "dept" : "DQ", "location": "NY" } {"username": "mary", "dept" : "TY", "location": "TA" } [/code] я хочу сделать запрос elasticsearch эквивалентным [code]select distinct username from my_index [/code] что даст мне результат: [code]["tom", "john", "mary"] [/code] Я попробовал эти ответы ElasticSearch — возврат уникальных значений И сделал запрос [code]query = { "size": 0, "aggs": { "unique_username": { "terms": { "field": "username.keyword", "size": 200 } } } } es.search(index="my_index", body=query) [/code] Это возвращает [code]{'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': []}}} [/code] с помощью запроса, согласно этим ответам, я ожидал уникальное имя пользователя с соответствующим количеством значений в списке сегментов, но список, похоже, пустой [code]{'buckets': []} [/code] Что я делаю не так? Также, когда я выполняю https://localhost:9200/my_index/_search?pretty=true&size=5 , я получаю результат { [code] "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" } } ] } } [/code] Заранее спасибо! Подробнее здесь: [url]https://stackoverflow.com/questions/68301422/python-elasticsearch-distinct-field-value-query-returning-empty-result[/url]