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.