【问题标题】:elasticsearch - Return term frequency of a single fieldelasticsearch - 返回单个字段的词频
【发布时间】:2015-11-05 21:19:14
【问题描述】:

我一直在尝试使用 facet 来获取字段的词频。我的查询只返回一个命中,所以我想让 facet 返回在特定字段中出现频率最高的术语。

我的映射:

{
"mappings":{
    "document":{
        "properties":{
            "tags":{
                "type":"object",
                "properties":{
                    "title":{
                        "fields":{
                            "partial":{
                                "search_analyzer":"main",
                                "index_analyzer":"partial",
                                "type":"string",
                                "index" : "analyzed"
                            }
                            "title":{
                                "type":"string",
                                "analyzer":"main",
                                "index" : "analyzed"
                            }
                        },
                        "type":"multi_field"
                    }
                }
            }
        }
    }
},

"settings":{
    "analysis":{
        "filter":{
            "name_ngrams":{
                "side":"front",
                "max_gram":50,
                "min_gram":2,
                "type":"edgeNGram"
            }
        },

        "analyzer":{
            "main":{
                "filter": ["standard", "lowercase", "asciifolding"],
                "type": "custom",
                "tokenizer": "standard"
            },
            "partial":{
                "filter":["standard","lowercase","asciifolding","name_ngrams"],
                "type": "custom",
                "tokenizer": "standard"
            }
        }
    }
}

}

测试数据:

 curl -XPUT localhost:9200/testindex/document -d '{"tags": {"title": "people also kill people"}}'

查询:

 curl -XGET 'localhost:9200/testindex/document/_search?pretty=1' -d '
{
    "query":
    {
       "term": { "tags.title": "people" }
    },
    "facets": {
       "popular_tags": { "terms": {"field": "tags.title"}}
    }
}'

这个结果

"hits" : {
   "total" : 1,
    "max_score" : 0.99381393,
    "hits" : [ {
    "_index" : "testindex",
    "_type" : "document",
    "_id" : "uI5k0wggR9KAvG9o7S7L2g",
    "_score" : 0.99381393, "_source" : {"tags": {"title": "people also kill people"}}
 } ]
},
"facets" : {
  "popular_tags" : {
  "_type" : "terms",
  "missing" : 0,
  "total" : 3,
  "other" : 0,
  "terms" : [ {
    "term" : "people",
    "count" : 1            // I expect this to be 2
   }, {
    "term" : "kill",
    "count" : 1
  }, {
    "term" : "also",
    "count" : 1
  } ]
}

}

上面的结果不是我想要的。我想让频率计数为 2

"hits" : {
   "total" : 1,
   "max_score" : 0.99381393,
   "hits" : [ {
   "_index" : "testindex",
   "_type" : "document",
   "_id" : "uI5k0wggR9KAvG9o7S7L2g",
   "_score" : 0.99381393, "_source" : {"tags": {"title": "people also kill people"}}
} ]
},
"facets" : {
"popular_tags" : {
  "_type" : "terms",
  "missing" : 0,
  "total" : 3,
  "other" : 0,
  "terms" : [ {
    "term" : "people",
    "count" : 2            
  }, {
    "term" : "kill",
    "count" : 1
  }, {
    "term" : "also",
    "count" : 1
  } ]
 }
}

我如何实现这一目标?刻面是不是走错路了?

【问题讨论】:

  • 请问我的回答是否有帮助?
  • 是的,真的很有帮助

标签: elasticsearch


【解决方案1】:

一个方面计算文档,而不是属于它们的术语。您得到 1,因为只有一个文档包含该术语,无论发生多少次都没有关系。我不知道返回术语频率的开箱即用方式,方面不是一个好的选择。
如果启用术语向量,该信息可以存储在索引中,但目前无法从弹性搜索中读取术语向量。

【讨论】:

【解决方案2】:

遗憾的是,Elastic 中没有字段的术语频率。 GitHub 项目Index TermList 正在使用 Lucene 的条款并计算所有文档的总出现次数,您可以检查它并根据您的需要进行替换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2012-10-22
    • 1970-01-01
    • 2012-07-03
    • 2015-06-11
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多