ruby-on-rails-4 - 嵌套排序无法按预期工作 ElasticSearch

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

要求

  • 使用匹配 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
            }
          }
        }
      }
    }
  ]
}

结果顺序应该是第二个产品,然后是第三个产品第一个产品。如果我想遍历产品并打印 custom_field_values.value,我应该得到 ZPB .

最佳答案

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

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

现在我们可以将这个标准化器与我们的关键字字段类型一起使用:

字段:field_name,类型:'关键字',标准化器:'my_normalizer'

希望这对您有所帮助。

关于ruby-on-rails-4 - 嵌套排序无法按预期工作 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929872/

相关文章:

ruby-on-rails - Elasticsearch 的拼写检查 Ngram 不适用于 Rails

ruby-on-rails - 如果在 Controller 外部声明了方法,为什么这个 Rails 代码可以工作?

css - 自定义字体不会在 Firefox for Rails 4 应用程序中呈现

ruby-on-rails - Simpleform 中的多态关联

elasticsearch - 如何在elasticsearch.yml中编写相对路径?

elasticsearch - 如何使一个发现表链接到仪表板或其他地方的另一个发现表?

ruby - Elasticsearch Ruby Activerecord持久性模型URL术语搜索

ruby-on-rails - 采摘返回重复的结果

elasticsearch - Elasticsearch错误的距离计算和过滤器

ruby-on-rails - 如何在 Elasticsearch 中查询数组元素