Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 460
fanhats Avatar asked Dec 29 '25 15:12

fanhats


1 Answers

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 + "*" 
                            }
                        }
                    }
                ]
            }
        }
    }
}
like image 108
bittusarkar Avatar answered Jan 02 '26 12:01

bittusarkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!