查詢我有像這樣一些數據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中實現。
有人可以提供一些指示,以確定這是否可行以及如何開始?