4

I am using laravel 4.2. my database is mongodb. I have a table called products and a field in my db which is called brand that has a value of ABC in uppercase.

When using this query ['term' => ['brand' => 'ABC']] the result set is empty.

But when i tried using this ['term' => ['brand' => 'abc']] it is actually working and returning all the products with brand = 'ABC'.

My question is why elasticsearch cannot find uppercase letters?

BatScream
  • 19,260
  • 4
  • 52
  • 68
kriscondev
  • 765
  • 7
  • 21

1 Answers1

4

This is because your brand field is analyzed and thus ABC gets tokenized and indexed as abc, hence why a search for the term abc returns hits while ABC doesn't.

In order to change this behavior you may create a sub-field for your brand field that is not_analyzed and you'll be able to search for the uppercase form as well.

curl -XPUT localhost:9200/my_index/my_type/_mapping -d '{
    "properties": {
        "brand": {
            "type": "string",
            "fields": {
                "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}'

If you run the command above and re-index your data, you'll then be able to search for

['term' => ['brand.raw' => 'ABC']]

Note that you may also use match queries (see below) on your brand field and they'll return matches.

['match' => ['brand' => 'abc']]
['match' => ['brand' => 'ABC']]
Val
  • 207,596
  • 13
  • 358
  • 360