Prefix query with analyzer

I want to do a prefix term query against an analyzed field, and use a search analyzer (to lowercase/asciifolding).

Here some docs:

  • "Pétanque"
  • "concours de pétanque"

Query "petan" should give me only "Pétanque".

Any idea?

1 Like

Why do you specifically want to use a search analyzer? I don't think you can do this with just a search analyzer. You will need to have an analyzer at index time too. The following should work:

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "my_field": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}

PUT my_index/doc/1
{
  "my_field": "Pétanque"
}

PUT my_index/doc/2
{
  "my_field": "concours de pétanque"
}

GET my_index/_search
{
  "query": {
    "prefix": {
      "my_field": {
        "value": "petan"
      }
    }
  }
}

From https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html:

"Matches documents that have fields containing terms with a specified prefix (not analyzed)."

I want to analyze what user types "PéTANque" ==> "petanque".

For now I am calling analyze API to retrieve the terms, but wondering if is it possible to do that without.

In that case, you could use a match_phrase_prefix query. This query is analyzed.

GET my_index/_search
{
  "query": {
    "match_phrase_prefix": {
      "my_field": {
        "query": "PéTANque"
      }
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.