S
S
sokolnikov2015-09-10 01:21:16
elasticsearch
sokolnikov, 2015-09-10 01:21:16

How to search in elasticsearch by prefix?

I decided to study elasticsearch, and I immediately come across questions (before that, I only used Sphinx).
There is a certain index test , there is a type country , it contains documents with country names and their descriptions.
I make the first request:

curl -XGET 'http://localhost:9200/test/country/_search?pretty=true' -d '
{
 "query": {
   "match": {
     "country": "Украина"
    }
  }
}'

I get the expected response:
{
  "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":"Украина"}

Now I want to search not by exact match, but by prefix. I follow the example from the documentation ( https://www.elastic.co/guide/en/elasticsearch/refe... ):
curl -XGET 'http://localhost:9200/test/country/_search?pretty=true' -d '
{
 "query": {
   "prefix": {
     "country": "Украи"
    }
  }
}'

I get an empty response:
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

What am I doing wrong? How to search by prefix (also interested in searching by infix), how to set up this search (for example, to search if even one letter is entered).
Only I ask, do not advise any add-ons. I still want to understand at a low level, then it will be easier with abstractions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ildar N, 2018-05-04
@radli

You must use index and search parsers. Show mapping

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question