在Elasticsearch中查询父子关系的问题

时间:2016-02-11 09:12:39

标签: elasticsearch

进行以下child-father映射:

10

...这两位父母有{ids 11curl -X PUT 'localhost:9200/my_index/my_parent/10' -d '{ "title" : "Microsiervos - Discos duros de 10TB", "body" : "Empiezan a sacar DD de 30GB en el mercado", "source_id" : "27", }' curl -X PUT 'localhost:9200/my_index/my_parent/11' -d '{ "title" : "Microsiervos - En el 69 llegamos a la luna", "body" : "Se cumplen 3123 anos de la llegada a la luna", "source_id" : "27", }'

curl -XPUT 'localhost:9200/my_index/my_child/1234_10?parent=10' -d '{
  "user_id": "1234",
}'

curl -XPUT 'localhost:9200/my_index/my_child/1234_11?parent=11' -d '{
  "user_id": "1234",
}'

......还有这两个孩子:

_id

通过以下查询,我想获得user_id = 1234父亲的curl -XGET 'localhost:9200/my_index/my_parent/_search?pretty=true' -d '{ "_source" : "_id", "query": { "has_child": { "type": "my_child", "query" : { "query_string" : { "default_field" : "user_id", "query" : "1234" }}}}}'

ids

这会输出两个1011ids

现在我想在父类上搜索那些特定的curl -XGET 'localhost:9200/my_index/my_parent/_search?pretty=true' -d '{ "query": { "bool": { "must": [ { "terms": { "_id": ["10", "11"] }}, { "query_string": { "default_field": "body", "query": "mercado" }}]}}}' ,如下所示:

"_id": ["10", "11"]

您可以注意到,ids部分是手写的。我想知道是否有办法将这两个查询合并到一个查询中,将第一个查询中返回的 }, "hits" : { "total" : 1, "max_score" : 0.69177496, "hits" : [ { "_index" : "my_index", "_type" : "my_parent", "_id" : "10", "_score" : 0.69177496, "_source":{ "title" : "Microsiervos - Discos duros de 10TB", "body" : "Empiezan a sacar DD de 30GB en el mercado", "source_id" : "27" }}]}} 自动放在第二个查询中。

所以输出到的应该是:

/var/www/<theme_folder>/ -> /home/<user>/public_html/wp-content/themes/<link_theme_folder>

1 个答案:

答案 0 :(得分:1)

使用bool Query并将这两个条件放在must

curl -XGET "http://localhost:9200/my_index/my_parent/_search" -d'
{
"query": {
  "bool": {
     "must": [
        {
           "query_string": {
              "default_field": "body",
              "query": "mercado"
           }
        },
        {
           "has_child": {
              "type": "my_child",
              "query": {
                 "query_string": {
                    "default_field": "user_id",
                    "query": "1234"
                 }
              }
           }
        }
     ]
     }
  }
}'