【问题标题】:Elasticsearch sorting - not getting expected resultsElasticsearch 排序 - 没有得到预期的结果
【发布时间】:2020-09-12 21:50:56
【问题描述】:

我的弹性字段名称为“Amit 111”、“amit 111”、“Amit 222”。

我正在尝试使用以下方法对其进行排序:

   searchSourceBuilder.query(query).sort("name.keyword", SortOrder.ASC)

它返回结果为: “阿米特 111”、“阿米特 222”、“阿米特 111”

但我希望结果为: “阿米特 111”、“阿米特 111”、“阿米特 222”

请帮忙。

【问题讨论】:

    标签: java elasticsearch resthighlevelclient


    【解决方案1】:

    另一种方法是使用 fielddatatext 字段一样,您可以应用排序,链接 URL 的更多详细信息。

    Java 代码,您需要更改索引映射,如 java 代码后所示。

    searchSourceBuilder.query(query).sort("name", SortOrder.ASC)
    

    name 字段上启用字段数据创建索引

    {
      "mappings": {
        "properties": {
          "name": { 
            "type": "text",
            "fielddata": true
            }
          }
        }
      }
    

    索引示例文档

    {
      "name" : "amit 111"
    }
    
    {
      "name" : "Amit 111"
    }
    
    {
      "name" : "Amit 222"
    }
    

    name 字段上排序的搜索查询

    {
        "sort": [
            {
                "name": "asc"
            }
        ]
    }
    

    结果

     "hits": [
          {
            "_index": "key",
            "_type": "_doc",
            "_id": "1",
            "_score": null,
            "_source": {
              "name": "amit 111"
            },
            "sort": [
              "111"
            ]
          },
          {
            "_index": "key",
            "_type": "_doc",
            "_id": "2",
            "_score": null,
            "_source": {
              "name": "Amit 111"
            },
            "sort": [
              "111"
            ]
          },
          {
            "_index": "key",
            "_type": "_doc",
            "_id": "3",
            "_score": null,
            "_source": {
              "name": "Amit 222"
            },
            "sort": [
              "222"
            ]
          }
        ]
    

    【讨论】:

    • 但 fieldData 会导致开销,并且据我推测很昂贵。
    • @amitkumarsingh 感谢您的评论,是的,它很贵,但如果您没有太多数据,也可以,此选项由 Elasticsearch 提供,再次取决于您的用例是否使用它:)
    • @amitkumarsingh 也启用了字段数据。没有太多开销(代码复杂性)。如果您可以接受并支持解决问题的答案,那也很棒,TIA :)
    【解决方案2】:

    关键字字段按原样存储,因此关键字字段的排序区分大小写。Normalizer 带有小写过滤器可用于索引关键字字段。

    关键字字段的normalizer属性类似于analyzer 除了它保证分析链产生一个单一的 令牌。

    映射:

    {
      "settings": {
        "analysis": {
          "normalizer": {
            "my_normalizer": {
              "type": "custom",
              "filter": [
                "lowercase"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "normalizer": "my_normalizer"
              }
            }
          }
        }
      }
    }
    
    

    查询: name.keyword 上的排序和 name.keyword 上的术语查询都将不区分大小写

    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "name.keyword": {
            "order": "asc"
          }
        }
      ]
    }
    
    

    结果:"

    "hits" : [
          {
            "_index" : "index84",
            "_type" : "_doc",
            "_id" : "SBvLT3IB8mx5yKbJQ7EC",
            "_score" : null,
            "_source" : {
              "name" : "Amit 111"
            },
            "sort" : [
              "amit 111"
            ]
          },
          {
            "_index" : "index84",
            "_type" : "_doc",
            "_id" : "SRvLT3IB8mx5yKbJULFl",
            "_score" : null,
            "_source" : {
              "name" : "amit 111"
            },
            "sort" : [
              "amit 111"
            ]
          },
          {
            "_index" : "index84",
            "_type" : "_doc",
            "_id" : "ShvLT3IB8mx5yKbJaLFg",
            "_score" : null,
            "_source" : {
              "name" : "Amit 222"
            },
            "sort" : [
              "amit 222"
            ]
          }
        ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多