Elasticsearch父子条件提取子项

时间:2014-07-17 23:41:49

标签: elasticsearch parent-child

我有两个使用父子关系映射的类型文档,映射如下:

{
  "venue": {
    "properties": {
      "id": {
        "type": "long",
        "index": "not_analyzed"
      },
      "address": {
        "type": "string",
        "index": "analyzed"
      },
      "capacity": {
        "type": "integer",
        "index": "not_analyzed"
      },
      "rate_standard": {
        "type": "nested",
        "properties": {
          "rate": {
            "type": "double",
            "precision_step": 1,
            "index": "not_analyzed"
          },
          "minimum": {
            "type": "integer",
            "index": "not_analyzed"
          }
        }
      }
    }
  },
  "calendar": {
    "_parent": {
      "type": "venue"
    },
    "properties": {
      "day": {
        "type": "date",
        "format": "yyyy-MM-dd",
        "precision_step": 1,
        "index": "not_analyzed"
      },
      "is_available": {
        "type": "boolean",
        "index": "not_analyzed"
      },
      "rate": {
        "type": "double",
        "precision_step": 1,
        "index": "not_analyzed"
      },
      "minimum": {
        "type": "integer",
        "index": "not_analyzed"
      }
    }
  }
}

我想对它们运行查询,如下所示:

  • capacity gte 2
  • 地址包含mampang jakarta indonesia的所有单词
  • 不得包含属性is_available = falsebetween 2014-01-01 to 2014-12-01
  • 的日历条目(子)

曾尝试构建查询,但它似乎搞乱了嵌套bool查询

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "capacity": {
              "gte": 2
            }
          }
        },
        {
          "match": {
            "address": {
              "query": "mampang jakarta indonesia",
              "operator": "and"
            }
          }
        }
      ],
      "must_not": {
        "has_child": {
          "type": "calendar",
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "available": false
                  }
                },
                {
                  "range": {
                    "day": {
                      "gte": "2014-01-01",
                      "lte": "2014-12-01"
                    }
                  }
                }
              ],
              "must_not": {},
              "should": {}
            }
          }
        }
      },
      "should": {}
    }
  }
}

1 个答案:

答案 0 :(得分:0)

这个似乎有效

{
  "query": {
    "bool": {
      "must": [
        { "range" : {"capacity": { "gte": 2 }} },
        { "match": {"address": { "query" : "mampang jakarta indonesia", "operator" : "and" }} }
      ],
      "must_not": [
        {
            "has_child": {       
                "type": "calendar",
                "query": {
                  "bool": {
                    "must":[
                        {"term": {"available" : false }},
                        {"range": {"day": {"gte": "2014-01-01", "lte": "2014-12-01" }}}
                      ]
                  }
                }
            }
        }
      ]
    }
  }
}

仍然不确定这是否是适当的查询来实现