如何获取elasticsearch索引中所有术语的频率

问题描述 投票:0回答:1

我有一个已索引的文档语料库。我还在索引时存储了术语向量。现在我想检索满足某些过滤选项的所有文档的术语向量。 通过提供文档 ID,我能够获取单个文档或一组文档的术语向量。但是有没有一种方法可以在不提供文档 ID 的情况下获取所有文档的术语向量呢? 最终我想做的是获取索引(即词袋矩阵)中所有文档的字段中所有术语的频率计数。

我正在使用elasticsearch-py作为客户端。

感谢任何指点。谢谢!

elasticsearch elasticsearch-py
1个回答
0
投票

您想通过词袋矩阵实现什么目的?有许多开箱即用的方法可以将句子嵌入到 elastic 中并执行跳过 BOW 方法的向量/语义搜索。请在此处查看此博客:https://www.elastic.co/search-labs/blog/articles/text-similarity-search-with-vectors-in-elasticsearch

如果您只是想计算矩阵,我认为最好的方法是在索引时或作为稍后的附加管道步骤使用 python 脚本来完成。 您可以使用 scikit learn 中的 CountVectorized 例如:https://scikit-learn.org/stable/modules/ generated/sklearn.feature_extraction.text.CountVectorizer.html

然后您仍然可以将计数/每个向量添加到索引中的文档中。

from sklearn.feature_extraction.text import CountVectorizer
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
vectorizer.get_feature_names_out()
print(X.toarray())

© www.soinside.com 2019 - 2024. All rights reserved.