Elasticsearch:按字母顺序排序,忽略数字和特殊字符

时间:2018-12-20 15:09:28

标签: java elasticsearch

如何按字母顺序对文本字段进行排序,而忽略特殊字符和数字?默认情况下,特殊字符排在后面,然后是数字和字母。

我需要的是字母应该排序并首先出现,然后是数字和特殊字符。在 ES 6.3 中甚至可能吗?

我已尝试使用自定义分析器替换所有非字母字符,但无法正常工作:

{
  "analysis": {
    "analyzer": {
      "alphabets_analyzer": {
        "tokenizer": "standard",
        "type": "keyword",
        "char_filter": [
          "alphabets_char_filter"
        ]
      }
    },
    "char_filter": {
      "alphabets_char_filter": {
        "type": "pattern_replace",
        "pattern": "[^a-zA-Z\\s\\.]",
        "replacement": ""
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我能够通过以下索引级别的设置来解决此问题:

{
  "analysis": {
    "analyzer": {
      "alphabetsStringAnalyzer": {
        "tokenizer": "standard",
        "filter": "lowercase",
        "type": "custom",
        "char_filter": [
          "alphabets_char_filter"
        ]
      }
    },
    "char_filter": {
      "alphabets_char_filter": {
        "type": "pattern_replace",
        "pattern": "[^a-zA-Z]",
        "replacement": ""
      }
    }
  }
}

并在索引映射中设置此分析器,如下所示:

"fullName":{
            "type": "keyword",
            "fields": {
                 "raw": {
                     "type": "text",
                     "analyzer": "alphabetsStringAnalyzer",
                     "fielddata": true
                 }
             }
          }