首页
学习
活动
专区
工具
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
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11806584

复制
相关文章

相似问题

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