Benjamin M
Benjamin M

Reputation: 24567

Elasticsearch Query String with Dot/Point at the end, i.e. +foo.*

I have an index containing lot's of streets. The index looks like this:

Mainstreet 42
Some other street 15
Foostr. 9

The default search query looks like this:

+QUERY_STRING*

So querying for foo (sent as +foo*) or foostr (sent as +foostr*) results in Foostr. 9, which is correct. BUT querying for foostr. (which get's sent to Elasticsearch as +foostr.*) gives no results, but why?

I use standard analyzer and the query string with no special options. (This also returns 0 results when using http://127.0.0.1:9200/test/streets?q=+foostr.*).

Btw. this: http://127.0.0.1:9200/test/streets?q=+foostr. (same as above without the asterisk) finds the right results

Questions:

  1. Why is this happening?

  2. How to avoid this behavior?

Upvotes: 0

Views: 8514

Answers (2)

Benjamin M
Benjamin M

Reputation: 24567

One thing i didn't think about was:

Elasticsearch will not analyze wildcard queries by default!

This means. By default it will act like this:

input query | the query that ES will use
----------------------------------------
foo         | foo
foo.        | foo
foo*        | foo*
foo.*       | foo.*

As you can see, if the input query contains a wildcard, ES will not remove any characters. When using no wildcard, ES will take the query and run an analyzer, which (i.e. when using the default analyzer) will remove all dots.

To "fix" this, you can either

Upvotes: 4

karthik manchala
karthik manchala

Reputation: 13640

1) This is because standard analyser does not index special characters. Example if you index a string Yoo! My name is Karthik., elasticsearch breaks it down to (yoo, my, name, is, karthik) without special characters (which actually makes sense in many simple cases) and in lowercase. So, when you search for foostr., there were no results.. as it was indexed as foostr (without ".").

2) You can use different types of analysers for different fields depending on your requirement while indexing (or you can use no_analyser as well).

Example:-

$ curl -XPUT 'http://localhost:9200/bookstore/book/_mapping' -d '
{
    "book" : {
        "properties" : {
            "title" : {"type" : "string", "analyzer" : "simple"},
            "description" : {"type" : "string", "index" : "not_analyzed"}
        }
    }
}
' 

You can refer this and this for more information.

HTH!

Upvotes: 0

Related Questions