首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >弹性搜索查询中下级作为父级的字段

弹性搜索查询中下级作为父级的字段
EN

Stack Overflow用户
提问于 2012-08-04 14:48:33
回答 1查看 16.1K关注 0票数 20

我正在阅读elasticsearch的文档,这一页讨论了如何使用_parent将子类型映射到父类型。

如果我有一个叫email的孩子和一个叫account的父母在一起

每种类型中的字段:

代码语言:javascript
复制
account (http://localhost:9200/myapp/account/1)
========
id
name
some_other_info
state

email (http://localhost:9200/myapp/email/1?parent=1)
========
id
email

  • 如何搜索nameaccount字段和emailemail字段(假设stateaccount )有一种方法可以获取父级拥有的所有子级(特定类型或任何类型)?

<account>H116在索引子文档时,是否可以将父级作为对象属性传递到数据中,而不是将其作为查询字符串的一部分?H217

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

这在http://localhost:9200/myapp/account/_search上执行

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "name": "a"
          }
        },
        {
          "term": {
            "statuses": "active"
          }
        }
      ],
      "should": [
        {
          "has_child": {
            "type": "emailaddress",
            "query": {
              "prefix": {
                "email": "a"
              }
            }
          }
        }
      ]
    }
  }
}

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

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

  • 有一个搜索框,用户开始输入,搜索框autocompletes.
  • The user's
  • 将根据account或任何emailaddress类型的名称进行检查。
  • 如果accounts匹配,只需返回它们。如果emailaddress匹配,则在每次搜索中将其父account.
  • Limit返回最多x个(例如10)个帐户。

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

测试数据:

代码语言:javascript
复制
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"
}'

伊莫托夫的解决方案对我很有效。我找到的另一个解决方案是在account中查询status = active,然后对结果运行bool筛选器,并对子类型使用has_child,在bool筛选器内对name使用prefix

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-06 03:21:45

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

代码语言:javascript
复制
{
    "bool": {
        "must": [
            { 
                "text" : { "name": "foo" } 
            }, { 
                "term" : { "state": "active" } 
            }, {
                "has_child": {
                    "type": "email",
                    "query": {
                        "text": {"email": "bar" }
                    }
                }
            }
        ]
    }
}

此查询将仅返回父文档(不返回子文档)。您可以使用该查询返回的父id通过字段_parent查找该父id的所有子对象,该字段默认情况下被存储和索引。

代码语言:javascript
复制
{
    "term" : { "_parent": "1" } 
}

或者,您可以将结果限制为在字段email中包含单词bar的子项

代码语言:javascript
复制
{
    "bool": {
        "must": [
            { 
                "term" : { "_parent": "1" } 
            }, { 
                "text" : { "email": "bar" } 
            }
        ]
    }
}

我不认为在json中指定parent是可能的,除非您使用的是_bulk indexing

这就是如何使用问题中提供的测试数据来实现电子邮件查找:

代码语言:javascript
复制
#!/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
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11806584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档