简体   繁体   English

ElasticSearch查询创建

[英]ElasticSearch Query Creation

I am having an inordinate amount of trouble trying to structure an ElasticSearch query. 我在尝试构造ElasticSearch查询时遇到了很多麻烦。

I need to get the top 25 tweets based on the sum of two fields, favoritesCount and retweetCount . 我需要根据两个字段的总和( favoritesCount retweetCountretweetCount来获得前25条推文。 I also need to be able to specify a date range on the field postedCount . 我还需要能够在字段postedCount上指定日期范围。

So far the closest I have been able to come is 到目前为止,我最接近的是

 query = {
    'size' : 25,
    'fields' : ['retweetCount', 'favoritesCount', 'preferredUsername', 'body'],

    "query": {
        "range": { 
            "postedTime" : {
                'gte' : 'now-24M',
                'lte' : 'now'
            }
        }
    },
    "sort" : {
        "retweetCount" : {"order" : "desc"},
        "type" : "number",

    }

};

This query too many results and sorts on the total number of tweets with the same retweet count. 此查询的结果太多,并且对具有相同转发次数的转发总数进行排序。 I also cannot figure out why this query doesn't return just the fields specified in the query. 我也无法弄清楚为什么此查询不只返回查询中指定的字段。

Ideally, the query would return only the fields '['retweetCount', 'favoritesCount', 'preferredUsername', 'body'] 理想情况下,查询将仅返回字段[['retweetCount','favoritesCount','preferredUsername','body']

This is the query that you should trigger: 这是您应触发的查询:

curl -X GET 'http://localhost:9200/index_name/type/_search' -d '{
"size" : 25,
"fields" : ["retweetCount", "favoritesCount", "preferredUsername", "body"],

"query": {
    "range": { 
        "postedTime" : {
            'gte' : 'now-24M',
            'lte' : 'now'
        }
    }
},
"sort" : {
    "retweetCount" : {"order" : "desc"},
    "type" : "number",

}
}'

Your are embedding everything inside query hash. 您正在将所有内容嵌入查询哈希中。 This is not needed 不需要这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM