我认为,elasticsearch嵌套查询

时间:2015-08-24 12:57:28

标签: elasticsearch filter mapping nested-queries

我来自相关的数据库背景,这样的事情会很简单,但我无法弄明白。我一直在努力学习Elasticsearch一个星期左右,我试图找出我认为是嵌套查询的内容。以下是一些示例数据:

PUT /myindex/pets/_mapping
{
   "pets": {
      "properties": {
         "name": {
            "type": "string"
         },
         "pet": {
            "type": "nested", 
            "properties": {
               "name": {"type": "string"}
            }
         }
      }
   }
}

POST /myindex/pets/
{"pet": {"name": "rosco"}, "name": "sam smith"}

POST /myindex/pets/
{"pet": {"name": "patches"}, "name": "sam smith"}

POST /myindex/pets
{"pet": {"name": "rosco"}, "name": "jack mitchell"}

查询看起来只会返回匹配的文档:

  • 所有者名称是“sam smith”
  • 宠物名称是“rosco”

我尝试了boolmatchnestedfiltered/filter类型查询的混合匹配,但我只是不断收到错误。像这样的东西在错误中脱颖而出:

nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"nested\"];

以下是查询:

GET /myindex/pets/_search
{
  "query": {
    "match": {
      "name": "sam smith"
    },
    "nested": {
      "path": "pet",
      "query": {
        "match": {
          "pet.name": "rosco"
        }
      }
    }
  }
}

由于Elasticsearch的相关性质,我开始认为我无法针对此特定目标。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

男人,有时这些查询很棘手......这似乎有效:

GET /myindex/pets/_search
{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "name": "sam smith"
        }
      },
      "filter": {
        "nested": {
          "path": "pet",
          "query": {
            "match": {
              "pet.name": "rosco"
            }
          }
        }
      }
    }
  }
}