在弹性搜索查询中将子对象视为父对象的字段

F21*_*F21 20 parent-child elasticsearch

我正在阅读有关elasticsearch的文档,本[页面] [1]讨论了如何使用子类映射子类型_parent.

如果我有被称为email父母的孩子叫做account:

每种类型的字段:

account (http://localhost:9200/myapp/account/1)
========
id
name
some_other_info
state

email (http://localhost:9200/myapp/email/1?parent=1)
========
id
email
Run Code Online (Sandbox Code Playgroud)
  • 如何搜索的name领域accountemail领域email提供的是stateaccountactive

  • 有没有办法让父母拥有的所有孩子(某种类型或任何类型)?

  • 索引子文档时,是否可以将父对象作为对象属性传递给JSON数据,而不是作为查询字符串的一部分?


在尝试了imotov的建议之后,我提出了这个问题:

这是执行 http://localhost:9200/myapp/account/_search

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "name": "a"
          }
        },
        {
          "term": {
            "statuses": "active"
          }
        }
      ],
      "should": [
        {
          "has_child": {
            "type": "emailaddress",
            "query": {
              "prefix": {
                "email": "a"
              }
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是上面没有给我任何电子邮件匹配的帐户.

我想要的效果基本上是这样的:

  • 有一个搜索框
  • 用户开始输入,搜索框自动填充.
  • 根据accountemailaddress类型或任何类型的名称检查用户的查询.
  • 如果accounts匹配,只需返回它们.如果emailaddress匹配,则返回其父帐户.
  • 每次搜索限制为最多x(比方说10)个帐户.

所以,我基本上需要能够OR在两种类型之间进行搜索并返回父类型的匹配.


测试数据:

curl -XPUT http://localhost:9200/test/account/1 -d '{
    "name": "John Smith",
    "statuses": "active"
}'

curl -XPUT http://localhost:9200/test/account/2 -d '{
    "name": "Peter Smith",
    "statuses": "active"
}'

curl -XPUT http://localhost:9200/test/account/3 -d '{
    "name": "Andy Smith",
    "statuses": "active"
}'

//Set up mapping for parent/child relationship

curl -XPUT 'http://localhost:9200/test/email/_mapping' -d '{
    "emails" : {
        "_parent" : {"type" : "account"}
    }
}'

curl -XPUT http://localhost:9200/test/email/1?parent=1 -d '{
    "email": "john@smith.com"
}'

curl -XPUT http://localhost:9200/test/email/2?parent=1 -d '{
    "email": "admin@mycompany.com"
}'

curl -XPUT http://localhost:9200/test/email/3?parent=1 -d '{
    "email": "abcd@efg.com"
}'

curl -XPUT http://localhost:9200/test/email/4?parent=2 -d '{
    "email": "peter@peter.com"
}'

curl -XPUT http://localhost:9200/test/email/5?parent=3 -d '{
    "email": "andy@yahoo.com"
}'

curl -XPUT http://localhost:9200/test/email/6?parent=3 -d '{
    "email": "support@mycompany.com"
}'
Run Code Online (Sandbox Code Playgroud)

imotov的解决方案对我有用.我已经找到了另一种解决方案是查询accountS表示status = active,再运行一个bool对结果过滤器和使用has_child对孩子的类型和prefixname内部bool过滤器.

imo*_*tov 21

elasticsearch和关系数据库之间的一个重要区别是elasticsearch无法执行连接.在elasticsearch中,您始终在搜索单个索引或索引联合.但是在父/子关系的情况下,可以使用子索引上的查询来限制父索引中的结果.例如,您可以对account类型执行此查询.

{
    "bool": {
        "must": [
            { 
                "text" : { "name": "foo" } 
            }, { 
                "term" : { "state": "active" } 
            }, {
                "has_child": {
                    "type": "email",
                    "query": {
                        "text": {"email": "bar" }
                    }
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

此查询将仅返回父文档(不会返回子文档).您可以使用此查询返回的父ID来使用该字段查找此父项的所有子项,该字段_parent默认存储并编制索引.

{
    "term" : { "_parent": "1" } 
}
Run Code Online (Sandbox Code Playgroud)

或者,您只能将结果限制为包含bar字段中单词的子项email:

{
    "bool": {
        "must": [
            { 
                "term" : { "_parent": "1" } 
            }, { 
                "text" : { "email": "bar" } 
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

除非您使用_bulk索引,否则我认为不可能在json中指定parent .

这是使用问题中提供的测试数据实现电子邮件查找的方式:

#!/bin/sh
curl -XDELETE 'http://localhost:9200/test' && echo 
curl -XPOST 'http://localhost:9200/test' -d '{
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0
    },
    "mappings" : {
      "account" : {
        "_source" : { "enabled" : true },
        "properties" : {
          "name": { "type": "string", "analyzer": "standard" },
          "statuses": { "type": "string",  "index": "not_analyzed" }
        }
      },
      "email" : {
        "_parent" : {
          "type" : "account"
        },
        "properties" : {
          "email": { "type": "string",  "analyzer": "standard" }
        }
      }
    }
}' && echo

curl -XPUT 'http://localhost:9200/test/account/1' -d '{
    "name": "John Smith",
    "statuses": "active"
}'

curl -XPUT 'http://localhost:9200/test/account/2' -d '{
    "name": "Peter Smith",
    "statuses": "active"
}'

curl -XPUT 'http://localhost:9200/test/account/3' -d '{
    "name": "Andy Smith",
    "statuses": "active"
}'

//Set up mapping for parent/child relationship

curl -XPUT 'http://localhost:9200/test/email/1?parent=1' -d '{
    "email": "john@smith.com"
}'

curl -XPUT 'http://localhost:9200/test/email/2?parent=1' -d '{
    "email": "admin@mycompany.com"
}'

curl -XPUT 'http://localhost:9200/test/email/3?parent=1' -d '{
    "email": "abcd@efg.com"
}'

curl -XPUT 'http://localhost:9200/test/email/4?parent=2' -d '{
    "email": "peter@peter.com"
}'

curl -XPUT 'http://localhost:9200/test/email/5?parent=3' -d '{
    "email": "andy@yahoo.com"
}'

curl -XPUT 'http://localhost:9200/test/email/6?parent=3' -d '{
    "email": "support@mycompany.com"
}'

curl -XPOST 'http://localhost:9200/test/_refresh'
echo
curl 'http://localhost:9200/test/account/_search' -d '{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "statuses": "active"
          }
        }
      ],
      "should": [
        {
          "prefix": {
            "name": "a"
          }
        },
        {
          "has_child": {
            "type": "email",
            "query": {
              "prefix": {
                "email": "a"
              }
            }
          }
        }
      ],
      "minimum_number_should_match" : 1
    }
  }
}' && echo
Run Code Online (Sandbox Code Playgroud)