Elasticsearch 未分析无法进行排序

Elasticsearch not analyzed not working in sorting

我在 elasticsearch 中添加了未分析选项的映射,当我对结果进行排序时它不起作用,这是我使用 http://localhost:9200/_river/jdbc/_search

时显示的映射
 "mappings": {
    "jdbc": {
        "dynamic_templates": [
            { "notanalyzed": {
                  "match":              "*", 
                  "match_mapping_type": "string",
                  "mapping": {
                      "type":        "string",
                      "index":       "not_analyzed"
                  }
               }
            }
          ]
       }
   }  

但是当我对结果进行排序时,它会像

http://localhost:9200/jdbc/_search?pretty=true&sort=field:asc

{ 
    field: "McDermott Will Amery", 
},
sort: [
    "amery"
]
}

但我需要从字段中的起始词开始按 A-Z 排序结果

更新: meta

中的河流规范
http://localhost:9200/_river/jdbc/_meta

{
  "_index": "_river",
  "_type": "jdbc",
  "_id": "_meta",
  "_version": 1,
  "found": true,
  "_source": {
    "type": "jdbc",
    "jdbc": {
      "driver": "com.mysql.jdbc.Driver",
      "url": "jdbc:mysql://localhost:3306/dbname",
      "user": "user",
      "password": "pass",
      "sql": "SQL QUERY",
      "poll": "24h",
      "strategy": "simple",
      "scale": 0,
      "autocommit": true,
      "bulk_size": 5000,
      "max_bulk_requests": 30,
      "bulk_flush_interval": "5s",
      "fetchsize": 100,
      "max_rows": 149669,
      "max_retries": 3,
      "max_retries_wait": "10s",
      "locale": "in",
      "digesting": true
    },
    "mappings": {
      "jdbc": {
        "dynamic_templates": [
          {
            "notanalyzed": {
              "match": "*",
              "match_mapping_type": "string",
              "mapping": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          }
        ]
      }
    }
  }
}

我认为您的配置不符合您的要求。让我们重新开始。首先,让我们删除您的 _river 索引,然后我们将从头开始重新创建它:

curl -XDELETE localhost:9200/_river

现在让我们再次创建它,但这次使用 correct configuration,即:

  1. 您的映射需要在 jdbc.type_mapping 字段中
  2. 您需要指定一个目标 indextype您的数据将存储在哪里

这是它的样子

curl -XPUT 'localhost:9200/_river/jdbc/_meta' -d '{
    "type" : "jdbc",
    "jdbc": {
      "driver": "com.mysql.jdbc.Driver",
      "url": "jdbc:mysql://localhost:3306/dbname",
      "user": "user",
      "password": "pass",
      "sql": "SQL QUERY",                  <-- add your SQL query
      "poll": "24h",
      "strategy": "simple",
      "scale": 0,
      "autocommit": true,
      "bulk_size": 5000,
      "max_bulk_requests": 30,
      "bulk_flush_interval": "5s",
      "fetchsize": 100,
      "max_rows": 149669,
      "max_retries": 3,
      "max_retries_wait": "10s",
      "locale": "in",
      "digesting": true,
      "index": "your_index",               <-- add this
      "type": "your_type",                 <-- add this
      "type_mapping": {                    <-- add your mapping here
          "your_type": {                   <-- match this with "type" above
            "dynamic_templates": [{
               "notanalyzed": {
                  "match": "*",
                  "match_mapping_type": "string",
                  "mapping": {
                     "type": "string",
                     "index": "not_analyzed"
                  }
               }
            }]
         }
      }
    }
}'

然后当您的 SQL 查询运行时,它会将数据存储在 your_index 索引中并使用 your_type 映射类型。

最后,您可以使用以下查询来搜索您的数据:

 curl -XGET 'http://localhost:9200/your_index/your_type/_search?pretty=true&sort=field:asc'

更新

您还可以使用以下定义多字段的映射。然后您就可以在 not_analyzed 字段上排序并在 analyzed 字段上搜索:

"dynamic_templates": [{
   "multi": {
      "match": "*",
      "match_mapping_type": "string",
      "mapping": {
         "type": "string",
         "fields": {
            "raw": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}]

对名为 field 的字段的查询将是

curl -XGET 'http://localhost:9200/your_index/your_type/_search?pretty=true&q=field:Luke&sort=field.raw:asc'