弹性搜索通过另一个文档查找文档 [英] Elasticsearch find documents by another document
问题描述
我想在弹性搜索中搜索与id docId的给定文档完全相同的字段的文档。例如用户使用docId调用api,我想过滤文档,以便返回的所有文档都会在docId中执行某些参数。
I want to search documents in elasticsearch which have exactly same fields as the given document of id docId. For e.g. user calls the api with a docId, I want to filter docs such that all the docs returned fulfills some parameters in docId.
例如我可以像这样查询Elasticsearch:
For example can I query Elasticsearch like this:
POST similarTerms/_search
{
"fields": [
"_id", "title"
] ,
"filter": {
"query": {"match": {
"title": doc[docId].title
}}
},
"size": 30
}
我知道我可以用docId获取文档,然后我可以准备上述查询,但是避免网络跳跃,甚至几毫秒的时间改进是我的应用程序非常关心的。
I know I can fetch the document with docId and then I can prepare the above query, but can I avoid the network hop somehow as even milliseconds of time improvement is of great concern for my app.
谢谢
推荐答案
这是一个更像这样api的文本书。来自文档的报价:
This is a text-book scenario for "more like this" api. Quote from the docs:
更像这样(mlt)API允许获取一个像一个
指定的文档的文档。这是一个例子:
The more like this (mlt) API allows to get documents that are "like" a specified document. Here is an example:
$ curl -XGET
'http:// localhost:9200 / twitter / tweet / 1 / _mlt ?mlt_fields = tag,content& min_doc_freq = 1'
API简单地导致执行一个具有moreLikeThis
查询http参数将参数匹配到more_like_this
查询)。这意味着请求的正文可以可选地包括
搜索API中的所有请求体选项(aggs,from / to等等)
。在内部,更像这样的API相当于对more_like_this_field查询执行
布尔查询,每个
指定一个查询mlt_fields。
The API simply results in executing a search request with moreLikeThis query (http parameters match the parameters to the more_like_this query). This means that the body of the request can optionally include all the request body options in the search API (aggs, from/to and so on). Internally, the more like this API is equivalent to performing a boolean query of more_like_this_field queries, with one query per specified mlt_fields.
如果您计划使用一个文档进行测试(如我所做)仅用于测试,请确保您还设置 min_term_freq = 0
和 min_doc_freq = 0
: GET / my_index / locations / 1 / _mlt?min_term_freq = 0& min_doc_freq = 0
If you plan testing this (like I did) with one document only for test, make sure you also set min_term_freq=0
and min_doc_freq=0
: GET /my_index/locations/1/_mlt?min_term_freq=0&min_doc_freq=0
这篇关于弹性搜索通过另一个文档查找文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!