簡體   English   中英

搜索查詢以在禁用_source的Elasticsearch中檢索嵌套文檔

[英]Search query to retrieve nested documents in elasticsearch with _source disabled

我有以下映射

{
    "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 [字段[變異]不是葉字段]”

符合預期。

如何獲得變體對象?

預期結果。 我需要整體檢索變量對象,以便保留大小和顏色的關聯。 像“紅色”和“ XXL”一樣。

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

更新:此索引類型的源已禁用。

如果使用“ 源過濾” ,它將整體返回嵌套對象,您的查詢將是:

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

您應該使用此:

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

我使用elasticsearch v.5.1.1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM