elasticsearch 选择elasticseacrh中的特定列,elasticsearch,elasticsearch" /> elasticsearch 选择elasticseacrh中的特定列,elasticsearch,elasticsearch" />

elasticsearch 选择elasticseacrh中的特定列

elasticsearch 选择elasticseacrh中的特定列,elasticsearch,elasticsearch,我在弹性搜索中有一个json示例 "snum": 1, "dept" : 1 "cards": [ { "card_num": 1, "dimensions": { "width": 1, "depth": 4, "height": 1 }, other_card_details{ } ]

我在弹性搜索中有一个json示例

"snum": 1,
"dept" : 1
"cards": [
 {
  "card_num": 1,
  "dimensions": {
    "width": 1,
    "depth": 4,
    "height": 1
  },
  other_card_details{
  }
 ]
如何在弹性搜索中编写查询以仅从cards数组中获取维度

我的映射如下所示:

"properties": {
"snum": {"type": "integer"},
"dept": {"type": "integer"},
"cards": {
    "type": "nested",
    "properties": {
        "card_num": {"type": "integer"},
        "dimensions": {
            "properties": {
                "width": {"type": "float"},
                "depth": {"type": "float"},
                "height": {"type": "float"}
            }
        },
    },
}

}

如果我正确理解您的问题,并且您的目标是只返回查询响应中的cards.dimensions字段,则需要在搜索的_source参数中指定该字段

{
  "query": { ... },
  "_source": ["cards.dimensions"]
}
相关文件:

根据评论更新: 如果你的目标是得到一个JSON

[{
  "dimensions": { ... }
},{
  "dimensions": { ... }
}]
您需要对响应JSON进行后期处理,并提取所需的字段。仅使用Elasticsearch可以获得的最佳结果是过滤仅显示点击的响应字段:

GET elasticsearch-hostname:9200/index-name/_query?filter_path=hits.hits._source
{
  "_source": ["cards.dimensions"]
}
您将获得:

{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "cards" : [
            {
              "dimensions" : {
                "depth" : 4,
                "width" : 1,
                "height" : 1
              }
            }
          ]
        }
      }
    ]
  }
}
然后,您可以对响应进行后期处理以制作卡片。在根字段中标注尺寸。例如,通过使用jq:


我试过这个。但这是一个完整的数据,我只需要你能解释一下你所说的完整数据是什么意思吗?还有其他字段,如snum、dept?我通过您提到的查询获取整个json对象。我只想获取维度数据-维度:{width:1,depth:4,height:1},如果您的json响应应该只包含cards.dimensions字段和子字段,那么仅使用Elasticsearch无法实现这一点,但您需要对查询响应进行后期处理。你能得到的最好结果是删除除hits.hits之外的所有字段。\u source,它是你卡的根字段。dimensions objectHi@CodeCool,你的问题得到了回答吗?你的映射看起来像什么?添加了映射
curl -XGET "elasticsearch-hostname:9200/index-name/_search?filter_path=hits.hits._source" \
     -H 'Content-Type: application/json' \
     -d'{
          "_source": ["cards.dimensions"]
        }' \
    | jq '.hits.hits[]._source.cards'