Elasticsearch - 想要在特定字段可用或不可用的所有索引中按字段排序,如果不可用则避免它

Elasticsearch - Want to sort by field in all indices where that particular field available or not if not then avoid it

目前,基于评分获取结果,但我想做的是我想要基于 评分 + 字段状态的结果,值为 true/false.

如果值为 true 则需要导致优先级,但是 可能状态字段不存在 在所有索引中。

           "query" => [
                  'bool' => [
                     'filter' => $filter,
                     'must' => [
                     "multi_match" => [
                        'query' => "$string",
                        "type" => "cross_fields",
                        'fields' => ['field1','field2','field3'],
                        "minimum_should_match" => "80%"
                         ]
                    ]
                  ]
            ],
            "sort" => [
                    "_score",
                    [ "status" => ["order" => "desc","unmapped_type" => "boolean"] ]
            ],

但出现以下错误:

[type] => illegal_argument_exception
[reason] => Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [status] in order to load field data by uninverting the inverted index. Note that this can use significant memory.

有人帮我忽略该字段不可用的索引或解决此问题的任何其他方法吗?

正如聊天中所讨论的那样,问题的发生是由于@jilesh

忘记删除旧的索引映射,只更新发生这种情况的数据。

当您通过正确设置遇到以下错误时,以下答案是相关的

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [status] in order to load field data by uninverting the inverted index. Note that this can use significant memory.

在这种情况下,如果您想消除错误,请在字段上启用字段数据,但要注意它可能会导致性能问题。

阅读有关 field data on official site 的更多信息。

您可以在映射的 order 字段中启用它,如图所示。

{
  "properties": {
    "order": { 
      "type":     "text",
      "fielddata": true
    }
  }
} 

适合我

curl -X PUT -H "Content-Type: application/json" \
    -d '{"properties": {"format": { "type":"text","fielddata": true}}}' \
    <your_host>:9200/<your_index>/_mapping

在我的例子中,我不得不使用可聚合的 {fieldname}.keyword 字段。下面是一个使用 Nest .NET 的示例。

.Sort(s => s
    .Ascending(f => f.Field1.Suffix("keyword"))
    .Ascending(f => f.Field2.Suffix("keyword"))
    .Ascending(f => f.Field3.Suffix("keyword")))