BentCoder
BentCoder

Reputation: 12720

Elasticsearch starts with search by using Partial Matching queries

When I search for product1, I should only get:

Product1
Product1/Product1
Product1-Product
Product1\Product1

however I am getting these:

Product1
Product1/Product1
Product1-Product
Product1\Product1
Product-Product1
Product\Product1
Product Product1

so my example queries below run like ... name LIKE '*product1*' instead of ... LIKE 'product1*'. I've gone through the Partial Matching documentation but couldn't solve the problem. The name field is not_analyzed string in mapping.

{"query":{"prefix":{"name":"product1"}}}

{"query":{"wildcard":{"name":"product1*"}}}

{"query":{"regexp":{"name":"product1*"}}}

{"query":{"bool":{"must":[{"wildcard":{"name":"product1*"}}]}}}

{"query":{"bool":{"must":{"wildcard":{"name":"product1*"}}}}}

{"query":{"filtered":{"filter":{"bool":{"must":{"regexp":{"name":"product1*"}}}}}}}

My ES version (I know it is a bit old):

{
  "status" : 200,
  "name" : "Hannah Levy",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.7.2",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

Upvotes: 0

Views: 116

Answers (2)

Jai Sharma
Jai Sharma

Reputation: 733

Are you sure you must be searching for product1* and not Product1.* ? Basically you want to match any character following Product1 0 or moer times. Please read the documentation of regex used by lucene here.

PS: I tried out your use-case in kibana with my regex and it works as expected.

Upvotes: 0

dshockley
dshockley

Reputation: 1494

You didn't include your mapping, but I suspect the mapping of name is something like {"type": "string", "index": "analyzed"} (or the equivalent {"type": "string"} -- the default is analyzed). The matches are on individual terms (not the raw string), and the individual terms created when Product-Product1 is indexed with the standard analyzer, are Product and Product1. Of course Product1 matches.

You'll have to re-index with "index": "not_analyzed", or add a new field with that mapping. (Actually it looks like you may need some analysis if you want product1 to match Product1 -- in that case I think you want to use a custom analyzer with a keyword tokenizer and lowercase filter.)

Upvotes: 1

Related Questions