Обрабатывать ребенка как поле родителя в эластичном поисковом запросе.
Я читаю документы дляasticsearch, и в этом [page] [1] говорится о сопоставлении дочернего элемента с родительским типом с помощью_parent
.
Если у меня есть дети называютemail
привязанный к родителям называетсяaccount
:
Поля в каждом типе:
account (http://localhost:9200/myapp/account/1)
========
id
name
some_other_info
state
email (http://localhost:9200/myapp/email/1?parent=1)
========
id
email
How can I search on the name
field of account
and the email
field of email
provided that the state
of account
is active
?
Is there a way to get all the children (of a certain type or of any type) a parent owns?
When indexing a child document, is it possible to to pass the parent as an object property in the JSON data as opposed to it being part of the query string?
Попробовав предложение Имотова, я пришел к следующему запросу:
Это выполняется наhttp://localhost:9200/myapp/account/_search
{
"query": {
"bool": {
"must": [
{
"prefix": {
"name": "a"
}
},
{
"term": {
"statuses": "active"
}
}
],
"should": [
{
"has_child": {
"type": "emailaddress",
"query": {
"prefix": {
"email": "a"
}
}
}
}
]
}
}
}
Проблема состоит в том, что вышеупомянутое не дает мне никаких учетных записей, где электронная почта совпадает.
Эффект, который я хочу, заключается в следующем:
There is one search box Users start typing and the search box autocompletes. The user's query is checked against the name of theaccount
or any of the emailaddress
type.
If accounts
were matched, just return them. If emailaddress
were match, return its parent account.
Limit to a maximum of x (say 10) accounts for each search.
Итак, мне нужно быть в состоянииOR
поиск между 2 типами и возврат родительского типа совпадений.
Тестовые данные:
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": "[email protected]"
}'
curl -XPUT http://localhost:9200/test/email/2?parent=1 -d '{
"email": "[email protected]"
}'
curl -XPUT http://localhost:9200/test/email/3?parent=1 -d '{
"email": "[email protected]"
}'
curl -XPUT http://localhost:9200/test/email/4?parent=2 -d '{
"email": "[email protected]"
}'
curl -XPUT http://localhost:9200/test/email/5?parent=3 -d '{
"email": "[email protected]"
}'
curl -XPUT http://localhost:9200/test/email/6?parent=3 -d '{
"email": "[email protected]"
}'
решение имотова сработало для меня. Другое решение, которое я нашел, это запросaccount
с дляstatus = active
затем запуститеbool
отфильтровать результат и использоватьhas_child
на тип ребенка иprefix
наname
внутриbool
фильтр.