Elasticsearch:获取给定文档中短语的频率

8

测试数据:

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '{ "body": "this is a test" }'
curl -XPUT 'localhost:9200/customer/external/2?pretty' -d '{ "body": "and this is another test" }'
curl -XPUT 'localhost:9200/customer/external/2?pretty' -d '{ "body": "this thing is a test" }'

我的目标是获得文档中一个短语的频率。

我知道如何获得文档中术语的频率:

curl -g "http://localhost:9200/customer/external/1/_termvectors?pretty" -d'
{
        "fields": ["body"],
        "term_statistics" : true
}'

我知道如何使用match_phrase或span_near查询来计算包含给定短语的文档数量:

curl -g "http://localhost:9200/customer/_count?pretty" -d'
{
  "query": {
    "match_phrase": {
      "body" : "this is"
      }
    }    
}'

我如何访问短语的频率?


1
根据这个讨论:https://discuss.elastic.co/t/phrase-frequency-in-a-document-and-in-the-whole-collection/61616/3,听起来在ES层面上并不是真正可能的。 - chilladx
我找到了这个讨论,但我的理解是没有办法得到“所有文档中短语频率的总和”,这并不是我想要的。相反,我想要每个文档的短语频率。我有误解吗? - Gilles Cuyaubere
我们需要这些统计数据来开发自己的评分模型。这让我想到它是每个文档的统计数据,在请求期间计算得出的。 - chilladx
是的,我正在寻找每个文档的统计数据。你有什么想法吗? - Gilles Cuyaubere
这些短语有多长?如果它们有一定的长度,您可以使用 Shingles,在索引时生成所有 N-gram 的组合。然后,您可以查找这些标记的频率。 - drjz
没有固定的短语长度,但我可以生成最大长度的短语,并在我想匹配的短语列表上使用保留单词令牌过滤器。 - Gilles Cuyaubere
1个回答

1
你可以使用术语向量。如文档中所述。

Return values edit

Three types of values can be requested: term information, term statistics and field statistics. By default, all term information and field statistics are returned for all fields but no term statistics. Term information edit

term frequency in the field (always returned)
term positions (positions : true)
start and end offsets (offsets : true)
term payloads (payloads : true), as base64 encoded bytes

您需要达到术语频率-在示例中,您可以看到文档中约翰·多的频率。请注意,termvector会重复磁盘空间占用,适用于它的字段。


是的,但我需要将我的短语视为标记以获取其频率。在您提到的示例中使用了“关键字”分析器。根据@drjz的评论,我将尝试在使用术语向量之前实现自定义分析器(带有shingles)。 - Gilles Cuyaubere
@Gilles Cuyaubere 不,绝对不行。术语向量字段仅适用于文本字段,而不适用于关键字字段。正如我昨天建议的那样,请查看此示例 https://www.elastic.co/guide/en/elasticsearch/reference/5.4/docs-termvectors.html#_example_returning_stored_term_vectors 并使用john doe的per_field分析器进行查询。 - Lupanoide
在per_field分析器示例中,所选择的分析器是关键字分析器https://www.elastic.co/guide/en/elasticsearch/reference/5.4/analysis-keyword-analyzer.html。因此,“John Doe”文档中“John Doe”标记的频率为1。如果我们回到我的例子,这将给我“this is a test”在“this is a test”中的频率,而我实际上正在寻找“this is a test”中“this is”的频率。 - Gilles Cuyaubere
不,你没有理解诀窍。如果你仔细阅读示例,就会发现fullname字段的映射。它不是关键字,而是文本!但是,如果你想要一系列单词的统计信息,比如你的情况,你必须在不将它们拆分为单词的情况下进行查询,因此你使用关键字分词器作为搜索分析器-这不是字段的分词器,正如你所说的那样!-,这不是索引分析器。因此,你只能在索引分析器中生成shingles,并在搜索分析器中将一系列单词的频率作为单个标记进行查询。注意! - Lupanoide
如果您在termvector端点查询“this is”,而没有使用关键字作为搜索分析器,它将返回“this”的输出统计信息和“is”的统计信息。 - Lupanoide

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接