0

NOTE: An answer was found, see the end of this post and the comments for more information.

ElasticSearch (ver 6) is my kryptonite. I have been trying to add a sorting to a query for far too long and only getting errors in response:

  • No mapping
  • Fielddata is disabled on text fields by default. Set fielddata=true ...
    • when I try to set "fielddata=true" ... I get more errors
    • [field_sort] unknown field [fielddata], parser not found

I've tried ingore_unmapped and get the same "unknown field" error.

The ES documentation is so generic that it is unhelpful, and nothing here on SO has helped... I've looked at/read after running into various errors

So now...

Here is my current query that I use through Postman. I use Nest in my C# project to build this up. I know how to build queries if I can get the json structure right in Postman:

{
  "from": 0,
  "size": 50,
  "aggs": {
    "specs": {
      "nested": {
        "path": "specs"
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "specs.name",
            "size": 10
          },
          "aggs": {
            "specValues": {
              "terms": {
                "field": "specs.value",
                "size": 10
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "Id": {
                  "value": 1
                }
              }
            }
          ],
          "must_not": [
            {
              "terms": {
                "partId": []
              }
            }
          ],
          "should": [
            {
              "terms": {
                "picCode": [
                  "b02"
                ]
              }
            },
            {
              "terms": {
                "partId": []
              }
            }
          ]
        }
      }
    }
  }
}

The results I get back are like this:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 281,
        "max_score": 1,
        "hits": [
            {
                "_index": "09812734",
                "_type": "part",
                "_id": "1234:1",
                "_score": 1,
                "_source": {
                    "id": "1234:1",
                    "partId": 1234,
                    "mcfCostCenterId": 1,
                    "oemCode": "ABC",
                    "oemDescription": "Blah blah blah",
                    "oemPartCode": "123456",
                    "picCode": "B02",
                    "description": "other blah blah",
                    "isServiceable": false,
                    "marketingDescription": "this thing does stuff"",
                    "salesVolume": 0,
                    "searchSortOrder": 0,
                    "catalogSortOrders": [],
                    "specs": [
                        {
                            "name": "Color",
                            "value": "NA"
                        },
                        {
                            "name": "Diameter",
                            "value": "7.0000"
                        },
                        {
                            "name": "OtherSpec",
                            "value": "Q"
                        },
                        {
                            "name": "LastSpec",
                            "value": "FuBar"
                        }
                    ]
                }
            }
        ]
    },
    "aggregations": {
        "specs": {
            "doc_count": 18,
            "names": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "Length",
                        "doc_count": 4,
                        "specValues": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "2",
                                    "doc_count": 1
                                },
                                {
                                    "key": "3",
                                    "doc_count": 1
                                },
                                {
                                    "key": "8",
                                    "doc_count": 1
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

I need to sort on description or salesVolume in the results.

We have another query that does sorting, so I used that same code for this query, but get the above errors. That code builds up the json as follows:

{
  "from": 0,
  "size": 50,
  "sort": [
    {
      "foo.bar": {
        "missing": "_last",
        "order": "desc",
        "nested_filter": {
          "term": {
            "foo.bar": {
              "value": "frankenstein"
            }
          }
        },
        "nested_path": "_source"
      }
    }
  ],
  "aggs": {
    "specs": {
      "nested": {
        "path": "specs"
...

As I try to edit that to make it relevant to this search, I just get errors and no results.

EDIT: So, getting "salesVolume" is pretty easy because it isn't a text field.

{
  "from": 0,
  "size": 50,
  "sort": [
    {
      "salesVolume": {
        "missing": "_last",
        "order": "desc"
      }
    }
  ],
  "aggs": {
  ...

This sets up a sort order no problem. So, the crux is text... I just can't figure out where to set the fielddata to true without erroring out.

EDIT 2: In my DTO I have the field already set up with the keyword attribute.

public class MyClass
{
    public string Id
    [Text]
    public string ThisCode
    [Keyword]
    public string MySortableTextField
    ... //and so forth
}

The search will still not perform and errors out.

ANSWER: ^^^ The Keyword attribute worked. I was not placing this in the right project. We have a project that builds out all our db indexes and that was still using the Text attribute. Once a indexing was redone then I was able to apply the sort order without errors.

jeffcodes
  • 726
  • 2
  • 8
  • 20
  • for text fields used .keyword. If you will check the mapping using get mapping api you will see keyword field for text fields. These fields have type="keyword". link https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html for reference – jaspreet chahal Jun 22 '19 at 02:09
  • @jaspreetchahal See edit above. I'm using the keyword attribute. – jeffcodes Jun 24 '19 at 13:12
  • Can you add the sort query(after adjusting to current search)which you are using. In question you have mentioned sort on sales volume is working fine so it is throwing error on description? – jaspreet chahal Jun 24 '19 at 13:19
  • Using both `[Keyword]` or `[Text(Fielddata = true)]` I get this error: "illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [description] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead." – jeffcodes Jun 24 '19 at 13:40
  • Can you paste the mapping of description field using /_mapping – jaspreet chahal Jun 24 '19 at 13:42
  • Shoot. Looks like the actual mapping was in another project. I need to rebuild that project with the `[Keyword]` attribute applied and see what happens. It was just designated as `[Text]`. I totally forgot we have two projects that run our searches. – jeffcodes Jun 24 '19 at 13:53
  • Many times text field have sub property with type keyword created automatically. If it is present you will be able to use description.keyword(without any need to reindex) ex { "foo": { "type" "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } – jaspreet chahal Jun 24 '19 at 13:55
  • Ok, let me try again before re-indexing. – jeffcodes Jun 24 '19 at 13:57
  • Nope, this is not the case. Will need to reindex: "description": { "type": "text" } – jeffcodes Jun 24 '19 at 14:07
  • Running our indexer again with the `Keyword` attribute applied got me going. Thanks! – jeffcodes Jun 24 '19 at 19:35
  • Glad could be of help – jaspreet chahal Jun 25 '19 at 01:38

0 Answers0