# Prefix query with analyzer

**URL:** <https://discuss.elastic.co/t/prefix-query-with-analyzer/96852>\
**Category:** Elasticsearch\
**Created:** [August 12, 2017, 5:48pm UTC](https://discuss.elastic.co/t/prefix-query-with-analyzer/96852 "2017-08-12T17:48:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [August 12, 2017, 5:48pm UTC](https://discuss.elastic.co/t/prefix-query-with-analyzer/96852/1 "2017-08-12T17:48:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 15, 2017, 11:46am UTC](https://discuss.elastic.co/t/prefix-query-with-analyzer/96852/2 "2017-08-15T11:46:54Z")

</div>

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"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [August 16, 2017, 1:26pm UTC](https://discuss.elastic.co/t/prefix-query-with-analyzer/96852/3 "2017-08-16T13:26:12Z")

</div>

From [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html:](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.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 16, 2017, 2:36pm UTC](https://discuss.elastic.co/t/prefix-query-with-analyzer/96852/4 "2017-08-16T14:36:37Z")

</div>

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"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 13, 2017, 2:36pm UTC](https://discuss.elastic.co/t/prefix-query-with-analyzer/96852/5 "2017-09-13T14:36:49Z")

</div>

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