首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >elastic search版本6-7,用于排序的分析器不工作

elastic search版本6-7,用于排序的分析器不工作
EN

Stack Overflow用户
提问于 2021-06-28 19:11:17
回答 1查看 16关注 0票数 0

我正在创建以下my_cars索引

代码语言:javascript
运行
复制
PUT my_cars
{
  "settings": {
    "analysis": {
      "analyzer": {
        "sortable": {
          "tokenizer": "keyword",
          "filter": ["lowercase"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        },
        "analyzer": "sortable"
      }
    }
  }
}

当我检查映射时,它看起来很好:

代码语言:javascript
运行
复制
{
  "my_cars" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          },
          "analyzer" : "sortable"
        }
      }
    }
  }
}

但是现在,当我运行查询进行搜索和排序时

代码语言:javascript
运行
复制
GET my_cars/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "name.keyword": {
      "order": "asc"
    }
  }
}

首先显示大写/大写的结果,因此我认为分析器不能正常工作。我得到的结果如下:

代码语言:javascript
运行
复制
{
  "took" : 163,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my_cars",
        "_type" : "_doc",
        "_id" : "f1RLUnoBZEZpPd-TeK9L",
        "_score" : null,
        "_source" : {
          "name" : "Apples",
          "price" : 250
        },
        "sort" : [
          "Apples"
        ]
      },
      {
        "_index" : "my_cars",
        "_type" : "_doc",
        "_id" : "H7JLUnoBh60DJePfnpGB",
        "_score" : null,
        "_source" : {
          "name" : "Brocoli",
          "price" : 250
        },
        "sort" : [
          "Brocoli"
        ]
      },
      {
        "_index" : "my_cars",
        "_type" : "_doc",
        "_id" : "gFRLUnoBZEZpPd-Tyq9A",
        "_score" : null,
        "_source" : {
          "name" : "azus",
          "price" : 110
        },
        "sort" : [
          "azus"
        ]
      },
      {
        "_index" : "my_cars",
        "_type" : "_doc",
        "_id" : "gVRMUnoBZEZpPd-TAq-A",
        "_score" : null,
        "_source" : {
          "name" : "botpzus",
          "price" : 80
        },
        "sort" : [
          "botpzus"
        ]
      }
    ]
  }
}

正如你所看到的,小写的名字排在最后,我该如何解决这个问题呢?我已经基于THIS问题构建了我的分析器。但与这个问题的答案不同的是,我无法直接在keyword映射中添加分析器字段。我如何修复我的字母搜索,而不考虑大小写?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-28 19:22:13

解决方案是在name.keyword上使用use a normalizer

代码语言:javascript
运行
复制
PUT my_cars
{
  "settings": {
    "analysis": {
      "normalizer": {
        "sortable": {
          "filter": ["lowercase"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "normalizer": "sortable"
          }
        }
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68162310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档