2012-08-08 67 views
1

查询我有像这样一些数据elasticsearch:返回一个自定义字段,其中值取决于elasticsearch

account (http://localhost:9200/myapp/account/1) 
======== 
name 
state 
groups //groups is an array containing objects like so {id: 1, starred: true} 
     //the id is the id of the group type 

email (http://localhost:9200/myapp/email/1?parent=1) 
======== 
email 
primary //just a boolean to denote whether this email is primary or not 

group 
======== 
name 
description 

email s为的account孩子。

基于imotov's excellent answer,我能够运行在account.nameemail.email搜索,并在搜索前缀匹配以上2场返回所有account S:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "term": { 
      "statuses": "active" 
      } 
     } 
     ], 
     "should": [ 
     { 
      "prefix": { 
      "name": "a" 
      } 
     }, 
     { 
      "has_child": { 
      "type": "email", 
      "query": { 
       "prefix": { 
       "email": "a" 
       } 
      } 
      } 
     } 
     ], 
     "minimum_number_should_match" : 1 
    } 
    } 
} 

我想现在是时候做返回2个自定义字段对于每个结果:

  • 对于每个结果返回一个字段称为email,如果搜索是在email型匹配(这是子女account),返回该电子邮件,否则返回链接到该帐户的primary电子邮件,如果没有,则返回null

  • 对于每个结果返回一个称为group的字段。该字段的值应包含其标识存储在groups阵列中的加星号的group的名称。实质上:查找group.id其中group.starred在每个account.groups中都为真,然后从group类型的基于我们找到的id返回匹配的group.name

我一直在寻找script fields,但我不确定它是否能够为每次打击返回字段。我也不确定上述内容是否可以在ES中实现。

有人可以提供一些指示,以确定这是否可行以及如何开始?

回答

1

目前,根本无法访问has_childnested子句中的数据。

唯一的解决方案是获取一些数据,在客户端做出一些决定,然后获得更多的数据。

这里是我做了什么来实现它:

  • 运行上面的查询和找回数据。

  • 为了应对显示匹配的电子邮件或主电子邮件(在电子邮件类型运行):

    {"query": 
        {"bool":{ 
         "should":{ 
         { 
          "prefix":{ 
           "email" : "the query" 
          } 
         }, 
         { 
          "terms":{ 
           "_parent" : ["list", "of", "account", "ids"] 
          } 
         } 
         } 
        } 
        } 
    } 
    

基于以上的查询,我们可以得到被匹配的任何电子邮件地址搜索词。请记住设置上述查询中的字段以包含_parent

然后,我们可以使用array_diff()或PHP以外的其他语言的类似函数来区分上方和下方的父ID和原始查询。然后,这应该给我们一个没有电子邮件匹配的帐户列表。只需发送另一个请求即可获取这些帐户的主要电子邮件。

对于组,将查询发送到输入account和:

  • 约束_id到帐户ID列表。
  • 约束group.starredtrue

这应该为您提供加星标组的列表。为了获得更多的信息(姓名等),将查询发送到输入group和:

  • 约束_id到组以上的ID。

最后,做一些客户端处理将它放在一起,以便您的程序的下一部分更容易使用。

相关问题