【问题标题】:Nested sorting not working as expected ElasticSearch嵌套排序未按预期工作 ElasticSearch
【发布时间】:2019-05-24 14:05:55
【问题描述】:

要求

  • 使用与 custom_field_id 匹配的 custom_field_values 对产品进行排序

映射

{
  "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"},
      ]
    }
  }
}

预期

  • 我应该能够按custom_field_values.custom_field_id 对整个product 过滤器进行排序,按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 product1st product。如果我想循环浏览产品并打印custom_field_values.value,我应该得到ZPB

【问题讨论】:

  • 我还在query 中添加了嵌套的filter,现在可以为一些自定义字段值提供正确的结果,但是存储带有特殊字符的值的列仍然没有正确排序。
  • 为什么custom_field_id的值是字符串类型,但在schema中是整数?
  • @Raman 感谢您指出这一点,更新了问题。我手动准备了该数据,现在它是正确的。

标签: ruby-on-rails-4 elasticsearch elasticsearch-rails chewy-gem elasticsearch-ruby


【解决方案1】:

所以,问题出在区分大小写的数据上。 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'

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多