Search query with keywords who use AND, OR and quotes

Ask a question
1

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

I would like 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
offer bounty
add comment

0 Answer

Your Answer