嵌套排序未按预期运行elasticsearch

ippsafx7  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(337)

要求
使用与自定义\u字段\u id匹配的自定义\u字段\u值对产品进行排序
Map

{
  "mapping": {
    "product": {
      "properties": {
        "user_id": {
          "type": "integer"
        }
        "custom_field_values": {
          "type": "nested",
          "properties": {
            "custom_field_id": {
              "type": "integer"
            },
            "id": {
              "type": "integer"
            },
            "value": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            }
          }
        }
      }
    }
  }
}

示例数据

{
  {
    "_type": "product",
    "_source": {
      "user_id": 1,
      "custom_field_values": [
        { "id": 1, "custom_field_id": 1, "value": "A"},
        { "id": 2, "custom_field_id": 2, "value": "B"},
        { "id": 3, "custom_field_id": 3, "value": "C"},
      ]
    }
  },
  {
    "_type": "product",
    "_source": {
      "user_id": 2,
      "custom_field_values": [
        { "id": 4, "custom_field_id": 1, "value": "Y"},
        { "id": 5, "custom_field_id": 2, "value": "Z"},
      ]
    }
  },
  {
    "_type": "product",
    "_source": {
      "user_id": 3,
      "custom_field_values": [
        { "id": 6, "custom_field_id": 2, "value": "P"},
        { "id": 7, "custom_field_id": 3, "value": "Q"},
      ]
    }
  }
}

预期
我应该可以把整个 product 筛选依据 custom_field_values.custom_field_id ,排序依据 custom_field_values.value 示例查询

{
  "size":100,
  "from":0, 
  "query":{
    "bool":{
      "filter":{
        "match":{
          "user_id":1
        }
      }
    }
  },
  "sort":[
    {
      "custom_field_values.value.keyword":{
        "order":"desc",
        "nested":{
          "path":"custom_field_values",
          "filter":{
            "match":{
              "custom_field_values.custom_field_id": 2
            }
          }
        }
      }
    }
  ]
}

更新的查询

{
  "size":100,
  "from":0, 
  "query":{
    "bool":{
      "filter":{
        "match":{
          "user_id":1
        }
      }
    },
    "nested": { 
      "path": "comments",
      "filter": {
        "custom_field_values.custom_field_id": 2
      }
    }
  },
  "sort":[
    {
      "custom_field_values.value.keyword":{
        "order":"desc",
        "nested":{
          "path":"custom_field_values",
          "filter":{
            "match":{
              "custom_field_values.custom_field_id": 2
            }
          }
        }
      }
    }
  ]
}

结果顺序应为 2nd product ,那么 3rd product 以及 1st product . 如果我想在产品上循环打印 custom_field_values.value 我应该去 Z , P , B .

e5nqia27

e5nqia271#

所以,问题在于区分大小写的数据。https://www.elastic.co/guide/en/elasticsearch/reference/current/normalizer.html 解决了我的问题。

"settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  }

现在我们可以将此规范化器与关键字字段类型一起使用: field :field_name, type: 'keyword', normalizer: 'my_normalizer' 希望这有帮助。

相关问题