查询我有像这样一些数据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.name
和email.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中实现。
有人可以提供一些指示,以确定这是否可行以及如何开始?