$2.44

Search query with keywords who use AND, OR and quotes

Ask a question+
1

I’m working on a search engine with Elasticsearch, and i have two inputs keywords and excluded keywords.

I want to use quotes, AND, and OR in my two inputs for advanced search.

How can I do that?

Here what I’m try :

Keywords :

"foo bar" AND lorem OR ipsum
$searchQuery['body']['query']['bool']['should'][0]['bool']['must'][]['multi_match'] = [
                        'query' => "foo bar",
                        'type' => 'phrase',
                        "fields" => [
                            "content", "title"
                        ],
                    ];

$searchQuery['body']['query']['bool']['should'][0]['bool']['must'][]['multi_match'] = [
                            'query' => "lorem",
                            "fields" => [
                               "content", "title"
                            ],
                        ];

$searchQuery['body']['query']['bool']['should'][1]['bool']['must'][]['multi_match'] = [
                            'query' => "ipsum",
                            "fields" => [
                               "content", "title"
                            ],
                        ];

Results :

"lorem foo bar"  => true
"foo bar"  => false
"ipsum foo bar"  => true
It work but is it the right way and I'dont know how to add the excluded keywords with the same system (quotes, and, or ).

Example what I expecting :

Excluded keywords :

"test AND test2 OR test3"
"lorem foo bar"  => true
"lorem foo bar test"  => true
"lorem foo bar test3"  => false
"test lorem foo bar test2"  => false
add comment

0 Answer

Your Answer