Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Currently, Getting result based on scoring but what i want to do is i want a result based on scoring + Field Status with value true/false.

If value is true then needed that results in priority but there is possibility that status field is not exist in all indices.

           "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"] ]
            ],

But getting error below :

[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.

Anyone help me out to ignore for indices where that field not available or any other solution with this problem?

like image 957
jilesh Avatar asked May 27 '20 12:05

jilesh


People also ask

Are all fields indexed in Elasticsearch?

By default, Elasticsearch indexes all data in every field and each indexed field has a dedicated, optimized data structure. For example, text fields are stored in inverted indices, and numeric and geo fields are stored in BKD trees.

What is Elasticsearch default sort?

In Elasticsearch, the relevance score is represented by the floating-point number returned in the search results as the _score, so the default sort order is _score descending.

How does Elasticsearch sort work?

Elasticsearch comes with a good default out of the box. It sorts the results by relevance to the search query term, most relevant first. Elasticsearch measures the relevance score as a floating-point number called _score, and orders results in the descending order of their _score values.


1 Answers

As discussed in the chat, the issue happened due to @jilesh

forget to delete the old index mapping and only upate the data that's what this thing was occurring.

Below answer is relevant when you get below error with proper setup

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.

In that case, please enable the field data on the field if you want to get rid of the error but beware it can cause performance issues.

Read more about the field data on official site.

You can enable it in your order field in your mapping as shown.

{
  "properties": {
    "order": { 
      "type":     "text",
      "fielddata": true
    }
  }
} 
like image 54
Amit Avatar answered Oct 20 '22 17:10

Amit