gpt4 book ai didi

python-3.x - 如何在Elasticsearch中计算每个 token 的总数

转载 作者:行者123 更新时间:2023-12-02 23:17:10 26 4
gpt4 key购买 nike

我有一个请求Elastic

{  
"query":{
"bool":{
"must":[
{
"query_string":{
"query":"something1 OR something2 OR something3",
"default_operator":"OR"
}
}
],
"filter":{
"range":{
"time":{
"gte":date
}
}
}
}
}
}

我想在一个请求中使用 flex 搜索来计算所有文档中每个 token 的计数,例如:
something1: 26 documents
something2: 12 documents
something3: 1 documents

最佳答案

假设 token 与枚举不类似(即受约束的一组特定值(例如状态名称),这会使正确的映射使terms aggregation成为您的最佳选择),我认为与filters aggregation最接近的是:

POST your-index/_search
{
"query":{
"bool":{
"must":[
{
"query_string":{
"query":"something1 OR something2 OR something3",
"default_operator":"OR"
}
}
],
"filter":{
"range":{
"time":{
"gte":date
}
}
}
}
},
"aggs": {
"token_doc_counts": {
"filters" : {
"filters" : {
"something1" : {
"bool": {
"must": { "query_string" : { "query" : "something1" } },
"filter": { "range": { "time": { "gte": date } } }
}
},
"something2" : {
"bool": {
"must": { "query_string" : { "query" : "something2" } },
"filter": { "range": { "time": { "gte": date } } }
}
},
"something3" : {
"bool": {
"must": { "query_string" : { "query" : "something3" } },
"filter": { "range": { "time": { "gte": date } } }
}
}
}
}
}
}
}

响应如下所示:
{
"took": 9,
"timed_out": false,
"_shards": ...,
"hits": ...,
"aggregations": {
"token_doc_counts": {
"buckets": {
"something1": {
"doc_count": 1
},
"something2": {
"doc_count": 2
},
"something3": {
"doc_count": 3
}
}
}
}
}

关于python-3.x - 如何在Elasticsearch中计算每个 token 的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53390846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com