按elasticsearch中字段的单词数排序

sort by number of words of a field in elasticsearch

我的文档中有一个字符串字段。现在我需要根据该字段的字数对我的文档进行排序。我如何在 elasticsearch 中完成它?

像这样使用term aggregation:

curl -H GET http://loclahost:9200/index name/_search?pretty=1 -d' 
    {
        "aggs": {
            "genders": {
                "terms": {
                    "field": "gender"
                }
            }
        }
    }'

Note : for curl command check this

此处搜索字段gender并获取聚合桶中所有性别的结果,默认结果是排序的。

最好的办法是将令牌计数与原始字段一起存储。请在此处查看核心类型中的文档:http://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-core-types.html#token_count

然后您将按 field.word_count 排序(其中字段是 'parent' 属性)。

最好的方法是使用 token count type。 但是我们需要确保我们没有破坏原始字符串。为此,我们需要使用 multi field 并添加额外的字段来单独跟踪标记。

现在像下面这样的映射应该最适合我们

{
    "tweet" : {
        "properties" : {
            "name" : {
                "type" : "multi_field",
                "fields" : {
                    "wordCount" : {"type" : "token_count"},
                }
            }
        }
    }
}