0

I'm not very familiar with elasticsearch, and I want to implement a search like search in magento in mysql mode, which would find fields containing at least one word starting with one of words that was inputted by user. (Elasticsearch's fulltext search finds words only if a search term have almost same length as word)

For example, if user typed string "qwe asd", documents with next values in some searchable fields should be found:

test1 qwerty123123 zxc1

test2 321 qwerty123123 asdfgh123123 zxc2

test3 asdfgh123123 zxc3

test4 456 qwe zxc4

But next values should not be returned by search:

test1qwerty

test2qwe

And so on.

In MySQL this query looks like this:(MATCH (data_index) AGAINST ('qwe* asd*' IN BOOLEAN MODE))

After some googling I found out that such behavior in elasticsearch can be accomplished with such query:

$response = $client->search([
    'index' => 'test',
    'type' => 'product',
    'body' => [
        'query' => [
            'bool' => [
                'should' => [
                    [
                        'wildcard' => [
                            'name' => [
                                'value' => 'qwe*'
                            ]
                        ]
                    ],
                    [
                        'wildcard' => [
                            'name' => [
                                'value' => 'asd*'
                            ]
                        ]
                    ],
                    [
                        'wildcard' => [
                            'description' => [
                                'value' => 'qwe*'
                            ]
                        ]
                    ],
                    [
                        'wildcard' => [
                            'description' => [
                                'value' => 'asd*'
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
]);

In this example search is made by two fields "name" and "description", but I suppose it would better to merge all searchable fields in one. And if user will enter more words, more wildcard statements would be added.

I think that this would be work slow, so I want to know if there is better solution? And I want the document score to be the greater the more inputted words found in the fields of the document.

Also solutions with not exactly same behavior but similar are welcome.

If there are arguments why I should not do such thing at all, I would like to hear them too.

  • Does this answer your question? [How to partial match wildcard query with multiple words in elastic search?](https://stackoverflow.com/questions/63175934/how-to-partial-match-wildcard-query-with-multiple-words-in-elastic-search) – Will Jenkins Mar 23 '21 at 16:56
  • @WillJenkins in that question the search is made by two words as single string, by in my case words can be placed independently in any part of field in any order, and can present both, and also can present only one. But I'm not sure about answer with ngram. Can it be used in my case (and with which adjustments), or not? If I switch to ngram, won't it find words that ends with search string (I don't want to)? – Naruto Uzumaki Mar 23 '21 at 17:30

0 Answers0