qwertyui90
qwertyui90

Reputation: 201

Searching letter by letter in elastic search

i am using elasticsearch with python as client. I want to query through a list of companies. Say Company field values are

Gokl 
Normn 
Nerth

Scenario 1(using elasticsearch-dsl python)

s = Search(using=client, index="index-test") \
.query("match", Company="N") 

So when i put N in query match i don't get Normn or Nerth. I think its probably because of tokenization based on words.

Scenario 2(using elasticsearch-dsl python)

s = Search(using=client, index="index-test") \
.query("match", Company="Normn") 

When i enter Normn i get the output clearly. So how can i make the search active when i enter letter n as in above scenario 1.

Upvotes: 1

Views: 1538

Answers (3)

Sina Mehrabi
Sina Mehrabi

Reputation: 41

please read query types from here

for this case, you can use the code below:

s = Search(using=client, index="index-test").\
.query("match_phrase_prefix", Company="N")

you can use multi-match query for Company and Another field like this:

s = Search(using=client, index="index-test").\
.query("multi_match", query="N", fields=['Company','Another_field'],type='phrase_prefix')

Upvotes: 1

pkhlop
pkhlop

Reputation: 1844

If I understand correctly you need to query companies started with specific letter

In this case you can use this query

{
  "query": {
    "regexp": {
      "Company": "n.*"
    }
  }
}

Upvotes: 1

sandlb
sandlb

Reputation: 198

I think you are looking for a prefix search. I don't know the python syntax but the direct query would look like this:

GET index-test/_search
{
  "query": {
    "prefix": {
      "company": {
        "value": "N"
      }
    }
  }
}

See here for more info.

Upvotes: 1

Related Questions