嵌套JSON文档的ElasticSearch源过滤查询

nxagd54h  于 2023-01-12  发布在  ElasticSearch
关注(0)|答案(2)|浏览(172)

我在ElasticSearch中有以下JSON对象as _source。我需要根据条件筛选源对象。例如,我只需要具有applied_as ==“COMMISSION”的JSON

"_source": {
      "factor" : [
        {
          "some_amount_usd" : [
            {
              "applied_as" : "TCKT_CNT",
              "version" : "8",
              "factor_value" : "1.12",
              "start_date" : "2022-01-01"
            },
            {
              "applied_as" : "TCKT_CNT",
              "version" : "8",
              "factor_value" : "1.12",
              "start_date" : "2022-02-01"
            },
            {
              "applied_as" : "COMMISSION",
              "version" : "8",
              "factor_value" : "1.12",
              "start_date" : "2022-02-01"
            },
          ]
        }
      ]
    }

我正在使用此文档。https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-fields.html#source-filtering
我当前正在使用此查询,但没有成功。我错过了什么?

GET form_some_index/_search
{
  "query": {
    "match": {
      "factor.some_amount_usd.applied_as": "COMMISSION"
    }
  }

}
bcs8qyzn

bcs8qyzn1#

你可以像这样执行查询,但是这将返回整个factor obj

    • 查询:**
{
  "query": {
    "nested": {
      "path": "factor",
      "query": {
        "nested": {
          "path": "factor.some_amount_usd",
          "query": {
            "match": {
              "factor.some_amount_usd.applied_as": "COMMISSION"
            }
          }
        }
      }
    }
  }
}
    • 输出:**
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.9808291,
    "hits": [
      {
        "_index": "commision",
        "_id": "1",
        "_score": 0.9808291,
        "_source": {
          "factor": [
            {
              "some_amount_usd": [
                {
                  "applied_as": "TCKT_CNT",
                  "version": "8",
                  "factor_value": "1.12",
                  "start_date": "2022-01-01"
                },
                {
                  "applied_as": "TCKT_CNT",
                  "version": "8",
                  "factor_value": "1.12",
                  "start_date": "2022-02-01"
                },
                {
                  "applied_as": "COMMISSION",
                  "version": "8",
                  "factor_value": "1.12",
                  "start_date": "2022-02-01"
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

有关详细信息,请参阅official document

daolsyd0

daolsyd02#

您可以开始使用Nested Query

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "factor",
            "query": {
              "nested": {
                "path": "factor.some_amount_usd",
                "query": {
                  "bool": {
                    "must_not": [
                      {
                        "term": {
                          "factor.some_amount_usd.applied_as.keyword": {
                            "value": "COMMISSION"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

相关问题