fanhats
fanhats

Reputation: 797

Partial matching not working in this query

Why does the following only match exact, and not partial?

    body: {
        query: {
            filtered: {
                filter: {
                    bool: {
                        should: [
                            { query: { match: { "name": "*"+searchterm+"*" }}},
                        ]
                    }
                }
            }
        }
    }

"*"+searchterm+"*" should match any words that contains searchterm. ie,

item1
item2
0item

But it only matches words exact searchterm ie, only item. Why is this?

Upvotes: 3

Views: 5480

Answers (1)

bittusarkar
bittusarkar

Reputation: 6357

If the name field is using default analyzer then the asterisk wildcard characters are dropped during analysis phase. Hence you always get results where name is exactly sarchterm. You need to use a Wildcard query for matching any document where value of name field contains searchterm.

query: {
    filtered: {
        filter: {
            bool: {
                should: [
                    {
                        query: {
                            wildcard: {
                                "name": "*" + searchterm + "*" 
                            }
                        }
                    }
                ]
            }
        }
    }
}

Upvotes: 5

Related Questions