搜索查询以在弹出搜索中检索嵌套文档并禁用_source

时间:2014-10-03 15:31:57

标签: search lucene elasticsearch dsl

我有以下映射

{
    "cloth": {
                 "dynamic" : false,
                 "_source" : {"enabled" : false },
        "properties": {
            "name": {
                "type": "string",
                "index": "analyzed"
            },
            "variation": {
                "type": "nested",
                "properties": {
                    "size": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "color": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

我无法找到使用字段查询检索嵌套对象字段的方法。

{
    "fields" : ["name" , "variation.size", "variation.color"],
    "query" : {
        "nested" : {
            "path" : "variation",
            "query" : {
                "bool" : {
                    "must" : [
                        { "term" : { "variation.size" : "XXL" } },
                        { "term" : { "variation.color" : "red" } }
                        ]
                }
            }
        }
    }
}

以上查询返回

"_id" : "1",
  "_score" : 1.987628,
  "fields" : {
    "variation.size" : [ "XXL", "XL" ],
    "variation.color" : [ "red", "black" ],
    "name" : [ "Test shirt" ]
  }

当我尝试

"fields" : ["name" , "variation"]

我收到了错误

  

状态:400

     

原因:" ElasticsearchIllegalArgumentException [字段[变体]不是叶子字段]"

这是预期的。

如何获取变体对象?

预期结果。我需要整体检索变量对象,以便我可以保留大小和颜色的关联。喜欢" red"用" XXL"。

"variation" : { "XXL" , "red"}

更新:此索引类型的来源已停用。

2 个答案:

答案 0 :(得分:4)

如果您使用Source Filtering,它将返回嵌套对象作为一个整体,您的查询将是:

{
  "_source": [
    "name",
    "variation"
  ],
  "query": {
    "nested": {
      "path": "variation",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "variation.size": "XXL"
              }
            },
            {
              "term": {
                "variation.color": "red"
              }
            }
          ]
        }
      }
    }
  }
}

答案 1 :(得分:1)

你应该用这个:

"script_fields": {
"variation": {
  "script": {
    "inline": "doc['variation.size'].value + ' ' + doc['variation.red'].value"
  }
 }
}

我使用elasticsearch v.5.1.1