私はelasticsearchのドキュメントを読んでおり、この[ページ] [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
ofがである場合、のフィールドとの
name
フィールドを検索するにはどうすればよいですか?account
email
email
state
account
active
親が所有する(特定のタイプまたは任意のタイプの)すべての子を取得する方法はありますか?
子ドキュメントのインデックスを作成するときに、クエリ文字列の一部ではなく、JSONデータのオブジェクトプロパティとして親を渡すことはできますか?
imotovの提案を試した後、私はこのクエリを思いつきました:
これはで実行されますhttp://localhost:9200/myapp/account/_search
{
"query": {
"bool": {
"must": [
{
"prefix": {
"name": "a"
}
},
{
"term": {
"statuses": "active"
}
}
],
"should": [
{
"has_child": {
"type": "emailaddress",
"query": {
"prefix": {
"email": "a"
}
}
}
}
]
}
}
}
問題は、上記ではメールが一致するアカウントが得られないことです。
私が欲しい効果は本質的にこれです:
- 検索ボックスが1つあります
- ユーザーが入力を開始すると、検索ボックスがオートコンプリートされます。
account
ユーザーのクエリは、または任意のemailaddress
タイプの名前に対してチェックされます。- 一致した場合
accounts
は、それらを返すだけです。一致した場合emailaddress
は、その親アカウントを返します。 - 検索ごとに最大x(たとえば10)アカウントに制限します。
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": "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"
}'
imotovのソリューションは私のために働いた。私が見つけた別の解決策は、 account
sをクエリしてから、結果に対してフィルターをstatus = active
実行し、子タイプとフィルター内で使用することです。bool
has_child
prefix
name
bool