我正在阅读elasticsearch的文档,这一页讨论了如何使用_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
name
的account
字段和email
的email
字段(假设state
为account
)有一种方法可以获取父级拥有的所有子级(特定类型或任何类型)?<account
>H116在索引子文档时,是否可以将父级作为对象属性传递到数据中,而不是将其作为查询字符串的一部分?H217
在尝试了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"
}
}
}
}
]
}
}
}
问题是,上面没有给我任何帐户的电子邮件匹配。
我想要的效果基本上是这样的:
account
或任何emailaddress
类型的名称进行检查。accounts
匹配,只需返回它们。如果emailaddress
匹配,则在每次搜索中将其父account.所以,我基本上需要能够在两个类型之间进行OR
搜索,并返回匹配的父类型。
测试数据:
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"
}'
伊莫托夫的解决方案对我很有效。我找到的另一个解决方案是在account
中查询status = active
,然后对结果运行bool
筛选器,并对子类型使用has_child
,在bool
筛选器内对name
使用prefix
。
发布于 2012-08-06 03:21:45
elasticsearch和关系数据库之间的一个重要区别是elasticsearch不能执行连接。在elasticsearch中,您总是搜索单个索引或索引的联合。但是在父/子关系的情况下,可以使用对子索引的查询来限制父索引中的结果。例如,您可以对account
类型执行以下查询。
{
"bool": {
"must": [
{
"text" : { "name": "foo" }
}, {
"term" : { "state": "active" }
}, {
"has_child": {
"type": "email",
"query": {
"text": {"email": "bar" }
}
}
}
]
}
}
此查询将仅返回父文档(不返回子文档)。您可以使用该查询返回的父id通过字段_parent
查找该父id的所有子对象,该字段默认情况下被存储和索引。
{
"term" : { "_parent": "1" }
}
或者,您可以将结果限制为在字段email
中包含单词bar
的子项
{
"bool": {
"must": [
{
"term" : { "_parent": "1" }
}, {
"text" : { "email": "bar" }
}
]
}
}
我不认为在json中指定parent是可能的,除非您使用的是_bulk indexing。
这就是如何使用问题中提供的测试数据来实现电子邮件查找:
#!/bin/sh
curl -XDELETE 'http://localhost:9200/test' && echo
curl -XPOST 'http://localhost:9200/test' -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"mappings" : {
"account" : {
"_source" : { "enabled" : true },
"properties" : {
"name": { "type": "string", "analyzer": "standard" },
"statuses": { "type": "string", "index": "not_analyzed" }
}
},
"email" : {
"_parent" : {
"type" : "account"
},
"properties" : {
"email": { "type": "string", "analyzer": "standard" }
}
}
}
}' && echo
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/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"
}'
curl -XPOST 'http://localhost:9200/test/_refresh'
echo
curl 'http://localhost:9200/test/account/_search' -d '{
"query": {
"bool": {
"must": [
{
"term": {
"statuses": "active"
}
}
],
"should": [
{
"prefix": {
"name": "a"
}
},
{
"has_child": {
"type": "email",
"query": {
"prefix": {
"email": "a"
}
}
}
}
],
"minimum_number_should_match" : 1
}
}
}' && echo
https://stackoverflow.com/questions/11806584
复制相似问题