我有类似于SQL中的联合操作的查询。我需要为每个索引指定结果集的大小。例如,我想从第一个索引获取10条记录,从第二个索引获取15条记录。

我的查询:

{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must": [{
                            "match_phrase_prefix": {"userName": "ar" }
                        }]
                    }
                },
                {
                    "bool": {
                        "must": [{
                            "match_phrase_prefix": { "groupName": "ar" }
                        }]
                    }
                }
            ]
        }
    }
}

发送查询的网址:http://website.com:9200/user_data,group_data/_search
如果您有任何想法,我将不胜感激。
谢谢

最佳答案

我认为您无法通过简单的查询来做到这一点。

但是可以使用Top Hits aggregation做到这一点,它允许您通过存储桶聚合器按特定字段对结果集进行分组。您的情况应如下所示:

{
     "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must": [{
                            "match_phrase_prefix": {"userName": "ar" }
                        }]
                    }
                },
                {
                    "bool": {
                        "must": [{
                            "match_phrase_prefix": { "groupName": "ar" }
                        }]
                    }
                }
            ]
        }
     }, #Your query stills the same
     "size": 0, #This will bring back nothing within the field "hits", so you can focus in the "aggregations" field.
     "aggs": {
         "10_usernames": {
             "top_hits": {
                 "_source": {
                     "includes": [ "userName" ]
                 },
                 "size" : 10
             }
         },
         "15_groupames": {
             "top_hits": {
                 "_source": {
                     "includes": [ "groupName" ]
                 },
                 "size" : 15
             }
         }

     }
}

您会在“聚合”字段中看到结果。

希望这会有所帮助! :D

关于elasticsearch - 为Elasticsearch中的每个子查询指定大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57463447/

10-11 07:15
查看更多