# How to search in elasticsearch using prefix?

**URL:** <https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035>\
**Category:** Elasticsearch\
**Created:** [September 10, 2015, 1:32pm UTC](https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035 "2015-09-10T13:32:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_Sokolnikov](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@Petr\_Sokolnikov](https://discuss.elastic.co/u/Petr_Sokolnikov)\
**Post date:** [September 10, 2015, 1:32pm UTC](https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035/1 "2015-09-10T13:32:07Z")

</div>

Hello. I'm newbie in elasticsearch. Before I used Sphinx, and now decide to learn elasticsearch.

So, I have some index named **test** , and type named **country** with several documents.

I have run first query:

```
curl -XGET 'http://localhost:9200/test/country/_search?pretty=true' -d '
{
 "query": {
   "match": {
     "country": "Ukraine"
    }
  }
}'

```

And have received answer:

```
{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.24701709,
    "hits" : [ {
      "_index" : "test",
      "_type" : "country",
      "_id" : "AU-xLqS2KzVZz9Yq1Hgw",
      "_score" : 0.24701709,
      "_source":{"langId":1,"langUrl":"ru","langActive":1,"countryId":2,"countryUrl":"ukraine","countryActive":1,"country":"Ukraine"}

```

So, it is works correct. But now I want to search using prefix 'Ukra':

```
curl -XGET 'http://localhost:9200/test/country/_search?pretty=true' -d '
{
 "query": {
   "prefix": {
     "country": "Ukra"
    }
  }
}'

```

But have received answer without result:

```
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : []
  }
}

```

Why it is not work? What I doing wrong? I have used example from documentation [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)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 10, 2015, 1:43pm UTC](https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035/2 "2015-09-10T13:43:45Z")

</div>

because prefix queries are not analyzed.

You have indexed `ukraine` behind the scene so `Ukra` does not match.

Try with `ukra`.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [September 10, 2015, 2:00pm UTC](https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035/3 "2015-09-10T14:00:00Z")

</div>

> [@dadoonet](#):
>
> You have indexed ukraine behind the scene so Ukra does not match.

You could also specify the analyzer for country as `keyword` and then your original query would work. See the [create index][[Create index API | Elasticsearch Guide [8.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings)] api's section on mappings for one way to do this.

Elasticsearch's defaults are pretty friendly to full text search against English text using the `match` query. How your documents are analyzed into terms and which queries analyze their terms is super important to understand. I don't have a handy link for that but reply if you can't find one.

---

<div class="post-metadata">

**Author:** ![Petr\_Sokolnikov](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@Petr\_Sokolnikov](https://discuss.elastic.co/u/Petr_Sokolnikov)\
**Post date:** [September 10, 2015, 2:01pm UTC](https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035/4 "2015-09-10T14:01:40Z")

</div>

Thank you, "ukra" does work.

---

<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:** [July 5, 2017, 11:51pm UTC](https://discuss.elastic.co/t/how-to-search-in-elasticsearch-using-prefix/29035/5 "2017-07-05T23:51:15Z")

</div>


