【问题标题】:ElasticSearch - Sort does not workElasticSearch - 排序不起作用
【发布时间】:2023-03-10 17:58:01
【问题描述】:

我正在尝试进行搜索并对结果进行排序。但是,我收到一个错误,不知道为什么。

编辑 - 我将提供我的完整映射。

 "myindex": {
      "mappings": {
         "mytype": {
            "dynamic_templates": [
               {
                  // Dynamic templates here!
               }
            ],
            "properties": {
               "fieldid": {
                  "type": "keyword",
                  "store": true
               },
               "fields": {
                  "properties": {
                     "myfield": {
                        "type": "text",
                        "fields": {
                           "sort": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        },
                        "analyzer": "myanalyzer"
                     }                     
                  }
               },
               "isDirty": {
                  "type": "boolean"
               }
            }
         }
      }
   }
}

当我使用排序执行搜索时,如下所示:

POST /index/_search
{

  "sort": [
    { "myfield.sort" : {"order" : "asc"}}
  ]
} 

我收到以下错误:

{
   "error": {
      "root_cause": [
         {
            "type": "query_shard_exception",
            "reason": "No mapping found for [myfield.sort] in order to sort on",
            "index_uuid": "VxyKnppiRJCrrnXfaGAEfA",
            "index": "index"
         }
      ]
   "status": 400
}

我正在关注有关 elasticsearch 的文档。 DOCUMENTATION

我也检查了这个链接: DOCUMENTATION

有人可以帮我吗?

【问题讨论】:

  • 拨打curl -XGET localhost:9200/index会得到什么?
  • 我得到了映射。但问题不在于映射,而在于搜索。您可以在最后一条评论中查看解决方案。无论如何谢谢:)

标签: sorting elasticsearch nest elasticsearch-5


【解决方案1】:

嗯,可能是您的映射设置不正确。我跟着使用以下内容:

PUT /your-index/
{
    "settings": {
        "number_of_replicas": "1",
        "number_of_shards": "3",

        "analysis": {
            "customanalyzer": {
                "ID": {
                    "type": "custom",
                    "tokenizer": "keyword",
                    "filter": ["lowercase"]
                }
            }
        }
    },
    "mappings": {
        "thingy": {
            "properties": {
                "myfield": {
                    "type": "text",
                    "analyzer": "ID",

                     "fields": {
                        "sort": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

要仔细检查索引是否确实有myfield.sort 字段查看

GET /your-index/thingy/_mapping

然后,上传一些文档(不需要指定子字段,elasticsearch会为你做)

POST /your-index/thingy/
{
    "myfield": "some-value"
}

现在我可以使用以下内容进行搜索:

POST /your-index/thingy/_search
{
    "sort": [
        { "myfield.sort": { "order": "asc" } } 
    ]
}

所以一定要检查:

  • 命名/错字(你永远不知道)
  • 您的映射(是否有“myfield.sort”字段)
  • 您是否在正确的索引中搜索?

希望这会有所帮助

【讨论】:

  • 我将其称为multi_field 或子字段,而不是嵌套字段,因为这意味着已经不同了:)
  • 好点,我改了一点,但 multi_fields 似乎... elastic.co/guide/en/elasticsearch/reference/1.4/… 不见了? :D
  • multi_field types 消失了,即不再需要指定"type": "multi_field",但是能够以多种不同方式分析和索引字符串的原理是还在那里。人们仍然将此称为多字段或子字段(有更好的名称吗?):)
  • 我认为问题不在于映射。我编辑答案并提供我的完整映射。如果你愿意,我也可以提供我的设置。我看不出他们有什么问题。
  • 好吧,我解决了问题。如您所见,我的映射在一个名为“fields”的属性中包含我的“sort”字段。所以我需要这样做: POST /myindex/mytype/_search { "sort": [ { "fields.myfield.sort": { "order": "asc" } } ] }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2013-04-07
  • 2015-05-19
  • 2012-09-20
  • 2015-06-23
  • 2018-05-08
相关资源
最近更新 更多