条款查询机制可以通过ID以外的其他字段查询吗? [英] can terms lookup mechanism query by other field but id?

查看:86
本文介绍了条款查询机制可以通过ID以外的其他字段查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关条款的Elasticsearch官方网站:
https://www.elastic.co/guide/zh/elasticsearch/reference/2.1/query-dsl-terms-query.html

here is elasticsearch official website about terms: https://www.elastic.co/guide/en/elasticsearch/reference/2.1/query-dsl-terms-query.html

如我们所见,如果要进行术语查找机制查询,则应使用以下命令:

As we can see, if we want to do terms lookup mechanism query, we should use command like this:

curl -XGET localhost:9200/tweets/_search -d '{
  "query" : {
    "terms" : {
      "user" : {
        "index" : "users",
        "type" : "user",
        "id" : "2",
        "path" : "followers"
      }
    }
  }
}'

但是如果我想通过其他字段查询用户。
假设用户还有其他一些字段,例如名称,我是否可以使用术语查找机制通过给用户提供名称而不是id来查找推文。
我尝试使用以下命令:

But what if i want to do query by other field of users. Assume that users has some other fields such as name and can i use terms lookup mechanism finding the tweets by giving users name but not id. I have tried to use command like this:

curl -XGET localhost:9200/tweets/_search -d '{
  "query" : {
    "terms" : {
      "user" : {
        "index" : "users",
        "type" : "user",
        "name" : "Jane",
        "path" : "followers"
      }
    }
  }
}'  

,但是发生错误。
期待您的帮助。谢谢!

but it occurs error. Looking forward to your help. Thank you!

推荐答案

术语查找机制基本上是一种内置的优化,无需进行两个查询即可联接两个索引,即索引A中的一个用于获取要查找的ID,第二个索引中的索引用于获取具有索引B中的ID的文档。

The terms lookup mechanism is basically a built-in optimization to not have to make two queries to JOIN two indices, i.e. one in index A to get the ids to lookup and a second to fetch the documents with those ids in index B.

与SQL相反,这样的JOIN可以仅适用于 id 字段,因为这是通过GET调用从Elasticsearch唯一检索文档的唯一方法,这正是Elasticsearch将在<$ c $中执行的操作c>条款查找。

In contrary to SQL, such a JOIN can only work on the id field since this is the only way to uniquely retrieve a document from Elasticsearch via a GET call, which is exactly what Elasticsearch will do in the terms lookup.

因此,要回答您的问题,除了<$ c $之外,条款查找机制将不适用于其他任何字段c> id 字段,因为要检索的第一个文档必须是唯一的。在您的情况下,ES不知道如何为 name Jane 的用户获取文档,因为 name 只是用户文档中的一个字段,而绝不是用户 Jane 的唯一标识符。

So to answer your question, the terms lookup mechanism will not work on any other field than the id field since the first document to be retrieved must be unique. In your case, ES would not know how to fetch the document for the user with name Jane since name is just a field present in the user document, but in no way a unique identifier for user Jane.

这篇关于条款查询机制可以通过ID以外的其他字段查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆