Elasticsearch: search_as_you_type doesn't work with multi-field

Created on 7 May 2020  路  3Comments  路  Source: elastic/elasticsearch

Elasticsearch version (bin/elasticsearch --version):
Elasticsearch 7.6.1
Plugins installed: []
Nest 7.6.1
JVM version (java -version):
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
OS version (uname -a if on a Unix-like system):
Linux c87e626860d1 4.19.76-linuxkit #1 SMP Thu Oct 17 19:31:58 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Description of the problem including expected versus actual behavior:

Steps to reproduce:

Create _title_ of type search_as_you_type with field _sortable_

PUT my_test
{
  "mappings": {
    "properties": {
      "title": {
        "type": "search_as_you_type",
        "fields": {
          "sortable": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "lowercase_normalizer"
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "filter": [
            "lowercase"
          ],
          "type": "custom"
        }
      }
    }
  }
}

No error but the sortable field is not present in the mapping

{
  "my_test" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "title" : {
          "type" : "search_as_you_type",
          "max_shingle_size" : 3
        }
      }
    },
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "provided_name" : "my_test",
        "creation_date" : "1588843483989",
        "analysis" : {
          "normalizer" : {
            "lowercase_normalizer" : {
              "filter" : [
                "lowercase"
              ],
              "type" : "custom"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "fhsaD0XzRhalwzqMNAfrxQ",
        "version" : {
          "created" : "7060199"
        }
      }
    }
  }
}
:SearcSuggesters >bug Search

Most helpful comment

I also encountered this problem - I believe it's because search_as_you_type fields create their own sub-fields. One simple workaround for this is to move search_as_you_type field to sub-field as well, and define top field as text field (not indexed if it's not really used for anything), e.g.:

PUT my_test
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "index": false,
        "fields": {
          "prefix": {
            "type": "search_as_you_type"
          },
          "sortable": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

That said I think this should be solved either by merging user defined sub-fields with SAYT sub-fields (_Xgram, _index_prefix) or forbidding defining fields in SAYT fields definition (+documenting this shortcoming and workaround).

All 3 comments

Pinging @elastic/es-search (:Search/Suggesters)

I also encountered this problem - I believe it's because search_as_you_type fields create their own sub-fields. One simple workaround for this is to move search_as_you_type field to sub-field as well, and define top field as text field (not indexed if it's not really used for anything), e.g.:

PUT my_test
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "index": false,
        "fields": {
          "prefix": {
            "type": "search_as_you_type"
          },
          "sortable": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

That said I think this should be solved either by merging user defined sub-fields with SAYT sub-fields (_Xgram, _index_prefix) or forbidding defining fields in SAYT fields definition (+documenting this shortcoming and workaround).

@telendt Thank you!
In my case, I had to do both more like this and search as you type on the same field. However more like this only works on keywords. I followed your direction and was able to fix it. I had to set "index":true though.

Was this page helpful?
0 / 5 - 0 ratings