ElasticSearch:命中中乘法字段和内部命中中的字段

t0ybt7op  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(277)

我尝试将两个字段相乘,其中一个字段在“hits”中,另一个字段在“inner_hits”中。
假设我有以下数据。

{
  "cost": 2.26,
  "sizes": [
    {
      "ratio": 1,
      "guid": "sfgsfdaa-bec9-42ef-8b3e-54957acc97f3"
    },
    {
      "ratio": 2,
      "guid": "sfgsadfg-1cc2-4c73-84e9-fa6c180e5874"
    },
    {
      "ratio": 3,
      "guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46"
    }
  ]
}

“sizes”字段是嵌套类型的。2我使用下面的查询来按guid过滤。

{
  "query" : { 
    "nested": {
      "path": "sizes",
      "query": {
        "bool" : {
            "must" : [
                { "match": { "sizes.guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46" }}
            ]
        }
      },
      "inner_hits": {}
    }
  }
}

查询的结果如下

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 4.9041457,
    "hits" : [
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "3eEs43sBtPPJDcTfcaVG",
        "_score" : 4.9041457,
        "_source" : {
          "cost": 2.26, //field to be multipled
          "sizes": [
            {
              "ratio": 1,
              "guid": "sfgsfdaa-bec9-42ef-8b3e-54957acc97f3"
            },
            {
              "ratio": 2,
              "guid": "sfgsadfg-1cc2-4c73-84e9-fa6c180e5874"
            },
            {
              "ratio": 3,
              "guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46"
            }
          ]
        },
        "inner_hits" : {
          "sizes" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 4.9041457,
              "hits" : [
                {
                  "_index" : "test-index",
                  "_type" : "_doc",
                  "_id" : "3eEs43sBtPPJDcTfcaVG",
                  "_nested" : {
                    "field" : "sizes",
                    "offset" : 2
                  },
                  "_score" : 4.9041457,
                  "_source" : {
                    "ratio": 3, //field to be multipled
                    "guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

我的问题是,如何将“ratio”(在“hits”内=〉“hits”=〉“inner_hits”=〉“sizes”=〉“hits”=〉“hits”=〉"_source”=〉“ratio”)与“cost”(在“hits”内=〉“hits”=〉“_source”=〉“cost”)相乘呢?

zbsbpyhn

zbsbpyhn1#

比如

jq -r '(.[].hits.hits[].inner_hits...... | tonumber)*(.[].hits.hits[]._source.cost | tonumber)'

应该这样做。

相关问题