3

I've loaded the 'accounts.json' data from the following link into an ES instance on my machine:

https://www.elastic.co/guide/en/elasticsearch/reference/1.5/_exploring_your_data.html

This adds 1000 docs to the index 'bank' with the type 'account'. Simple enough!

Every doc has this structure:

{
    "account_number": 0,
    "balance": 16623,
    "firstname": "Bradshaw",
    "lastname": "Mckenzie",
    "age": 29,
    "gender": "F",
    "address": "244 Columbus Place",
    "employer": "Euron",
    "email": "[email protected]",
    "city": "Hobucken",
    "state": "CO"
}

Now I'm trying to run a simple 'prefix' query on this index.

Here is one that works just fine (comes back with plenty of correct results):

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "prefix" : { "address" : "963" } }
}'

Here is another one (this one doesn't work):

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "prefix" : { "firstname" : "Op" } }
}'

But there is definitely a record present which should be returned in the previous request. The following works:

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match" : { "firstname" : "Opal" } }
}'

I have also verified the mapping and there doesn't seem to be any difference in the 2 fields, 'firstname' and 'address':

curl -XGET 'localhost:9200/bank/_mapping/account?pretty'

Here is the relevant mapping portion for those 2:

 "address": {
     "type": "string"
 }

 "firstname": {
     "type": "string"
 }

Can't figure out why one prefix query works and the other one doesn't. Any pointers on what I'm missing?

1 Answer 1

14

I think you'll find that this will do what you expect:

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "prefix" : { "firstname" : "op" } }
}'

The reason is that, since you have not specified an analyzer, the standard analyzer is used, which converts tokens to lower-case.

Here is some code that I used to test out my suspicion:

http://sense.qbox.io/gist/a4f087cee78fd694dd4223eb56e842e1cd1d5847

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent! Thanks for the answer Sloan. That worked. I knew it was something like that.
Hey, this link is broken now :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.