es的text类型的排序问题

es版本6.8.4
解决方式:对字段索引两次,一次索引分词(用于搜索)一次索引不分词(用于排序)

  • 字符串类型排序报错演示:
##mapping创建
PUT test_text_sort
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }, 
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  }
}
##插入数据
PUT test_text_sort/doc/1
{
  "name":"aaaaa 1"
}
PUT test_text_sort/doc/2
{
  "name":"aaaaa 2"
}
PUT test_text_sort/doc/3
{
  "name":"aaaaa 3"
}

##查询并排序
GET test_text_sort/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name": {
        "order": "desc"
      }
    }
  ]
}
>>>结果报错
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "test_text_sort",
        "node": "0MqXXcTVQKySTK81OiGp_Q",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
        }
      }
    ],
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    }
  },
  "status": 400
}
  • 字符串类型排序实现演示
##删除索引
DELETE test_text_sort

## mapping创建
PUT test_text_sort
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text",
          "fields":{
            "row":{
                "type":"keyword" 
            }
          },
          "fielddata":true
        }
      }
    }
  }, 
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  }
}

##准备数据
PUT test_text_sort/doc/1
{
  "name":"aaaaa 1"
}
PUT test_text_sort/doc/2
{
  "name":"aaaaa 2"
}
PUT test_text_sort/doc/3
{
  "name":"aaaaa 3"
}

## 查询方式一(错误)
GET test_text_sort/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name": {
        "order": "desc"
      }
    }
  ]
}
>>>查询结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test_text_sort",
        "_type" : "doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "aaaaa 2"
        },
        "sort" : [
          "aaaaa"
        ]
      },
      {
        "_index" : "test_text_sort",
        "_type" : "doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "aaaaa 1"
        },
        "sort" : [
          "aaaaa"
        ]
      },
      {
        "_index" : "test_text_sort",
        "_type" : "doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "aaaaa 3"
        },
        "sort" : [
          "aaaaa"
        ]
      }
    ]
  }
}

##查询方式二 (正确)
GET test_text_sort/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name.row": {
        "order": "desc"
      }
    }
  ]
}
>>>查询结果
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test_text_sort",
        "_type" : "doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "aaaaa 3"
        },
        "sort" : [
          "aaaaa 3"
        ]
      },
      {
        "_index" : "test_text_sort",
        "_type" : "doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "aaaaa 2"
        },
        "sort" : [
          "aaaaa 2"
        ]
      },
      {
        "_index" : "test_text_sort",
        "_type" : "doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "aaaaa 1"
        },
        "sort" : [
          "aaaaa 1"
        ]
      }
    ]
  }
}

另外:通过es默认创建text字段时的结构可以看出,默认创建的额text字段也是通过这种方式来实现text类型的排序的

##直接让es默认创建index并实现默认的mapping
PUT defult_text/doc/1
{
  "name":"wangfei"
}

##查看mapping
GET defult_text/_mapping
>>>查看结果
{
  "defult_text" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

##添加两条数据
PUT defult_text/doc/1
{
  "name":"wangjifei"
}

PUT defult_text/doc/2
{
  "name":"angjifei"
}

## 排序查询,可以实现对text类型字段的排序
GET defult_text/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name.keyword": {
        "order": "asc"
      }
    }
  ]
}
>>>查看结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "defult_text",
        "_type" : "doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "wangjifei"
        },
        "sort" : [
          "wangjifei"
        ]
      },
      {
        "_index" : "defult_text",
        "_type" : "doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "angjifei"
        },
        "sort" : [
          "angjifei"
        ]
      }
    ]
  }
}

你可能感兴趣的:(es的text类型的排序问题)