Elasticsearch聚合不起作用

时间:2016-09-14 13:33:33

标签: elasticsearch

我的页面上有访问日志的索引。每个用户访问页面都会记录一行:

{
    "_index": "logs",
    "_type": "visit",
    "_id": "AVco3MoAdMBqjXxJcqfF",
    "_version": 1,
    "_score": 1,
    "_source": {
        "@version": "1",
        "@timestamp": "2016-09-14T13:22:20.074Z",
        "user": "309424",
        "page": "15399",
        "countryCode": "FR"
    }
}

我试图通过countryCode获取查看次数最多的网页

日志上的POST请求/访问/ _search?search_type = count:

 {  
    "aggs":{  
        "pages":{  
            "terms":{  
                "field":"page"
            }
        }
    },
    "query":{  
        "bool":{  
            "must":[  
                {  
                    "term":{  
                        "countryCode":{  
                            "value":"FR",
                            "boost":1
                        }
                    }
                }
            ]
        }
    }
}

但响应阵列"桶"是空的。而当我使用" user"进行查询时而不是" countryCode",我通过我指定的用户查看了大多数已查看页面的结果。但我需要按国家划分。

我的countryCode字段和用户字段之间的区别是什么?两者都声明为字符串

"countryCode": {
    "type": "string"
},
"user": {
    "type": "string"
}

1 个答案:

答案 0 :(得分:1)

您的countryCode字段是已分析的字符串,因此您的查询必须与此类似

{  
    "aggs":{  
        "pages":{  
            "terms":{  
                "field":"page"
            }
        }
    },
    "query":{  
        "bool":{  
            "must":[  
                {  
                    "term":{  
                        "countryCode":{  
                            "value":"fr",     <--- lowercase fr here
                            "boost":1
                        }
                    }
                }
            ]
        }
    }
}

或者您可以保留大写FR并使用match查询

{  
    "aggs":{  
        "pages":{  
            "terms":{  
                "field":"page"
            }
        }
    },
    "query":{  
        "bool":{  
            "must":[  
                {  
                    "match":{                <--- use match
                        "countryCode":{  
                            "value":"FR",
                            "boost":1
                        }
                    }
                }
            ]
        }
    }
}
相关问题