简体   繁体   English

如何在Elasticsearch中搜索多代

[英]How to search multiple generations in Elasticsearch

Elasticsearch's article outlines how to find objects based on a search through one generation: https://www.elastic.co/guide/en/elasticsearch/guide/current/grandparents.html Elasticsearch的文章概述了如何基于一代人的搜索来查找对象: https : //www.elastic.co/guide/en/elasticsearch/guide/current/grandparents.html

GET /company/country/_search
{
  "query": {
    "has_child": {
      "type": "branch",
      "query": {
        "has_child": {
          "type": "employee",
          "query": {
            "match": {
              "hobby": "hiking"
            }
          }
        }
      }
    }
  }
}

What if I want to also want to query the branch for the name starting with "liverpool"? 如果我还想查询分支以“ liverpool”开头的name怎么办? How do you modify this search to find that? 您如何修改此搜索以找到该内容? I keep getting format errors and I can't seem to find information about how nest the queries online. 我一直收到格式错误,但似乎找不到有关如何在线嵌套查询的信息。

I've already tried this (it doesn't work): 我已经尝试过了(它不起作用):

GET /company/country/_search
{
  "query": {
    "has_child": {
      "type": "branch",
      "query": {
        "has_child": {
          "type": "employee",
          "query": {
            "match": {
              "hobby": "hiking"
            }
          }
        },
        "match": {
          "name": "london"
        }
      }
    }
  }
}

I got an error that said the query was malformed. 我收到一个错误,指出查询格式错误。

I tried bool , but it doesn't work either; 我试过bool ,但是也没有用; I can't use has_child with a bool. 我不能在布尔中使用has_child。

I was able to make it work with a bool query . 我能够使其与bool查询一起使用

If I'm understanding your question correctly, this should do what you want: 如果我正确理解了您的问题,那么您应该执行以下操作:

POST /company/country/_search
{
   "query": {
      "has_child": {
         "type": "branch",
         "query": {
            "bool": {
               "must": [
                  {
                     "has_child": {
                        "type": "employee",
                        "query": {
                           "match": {
                              "hobby": "hiking"
                           }
                        }
                     }
                  },
                  {
                     "match": {
                        "name": "liverpool"
                     }
                  }
               ]
            }
         }
      }
   }
}

If you want the query to be an "OR" instead of an "AND", use "should" instead of "must" . 如果您希望查询是“ OR”而不是“ AND”,请使用"should"而不是"must"

Here is some code I set up to play around with the problem: 这是我设置的一些解决该问题的代码:

http://sense.qbox.io/gist/cf4babbbd25a3b7a26f3ea9ce9c1b686e37dbcb9 http://sense.qbox.io/gist/cf4babbbd25a3b7a26f3ea9ce9c1b686e37dbcb9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM