我想在 elasticsearch 中搜索与 id docId 的给定文档具有完全相同字段的文档。例如用户使用 docId 调用 api,我想过滤文档,以便返回的所有文档都满足 docId 中的某些参数。
例如,我可以这样查询 Elasticsearch 吗:
POST similarTerms/_search
{
"fields": [
"_id", "title"
] ,
"filter": {
"query": {"match": {
"title": doc[docId].title
}}
},
"size": 30
}
我知道我可以使用 docId 获取文档,然后我可以准备上述查询,但我能否以某种方式避免网络跳转,因为即使是毫秒级的时间改进对我的应用程序来说也是非常重要的。
谢谢
最佳答案
这是“更像这样”api 的教科书场景。引自 the docs :
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'
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
关于Elasticsearch 通过另一个文档查找文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26977932/