Reputation: 49
How can I search a data in elasticsearch. I mean If I putt any data in text field than the related data from that word should be search and displayed. What I have tried:
$name = $this->getRequest()->getPost('name');
if(isset($name) && !empty($name)) /*checking condition */
{
$searchParams['body']['query']['bool']['must'][]['term'] ['couchbaseDocument.doc.name']= '*$name*'; /* elasticsearch query for filter the partial name
}
Upvotes: 0
Views: 1061
Reputation: 31
If u need to search like What you have mentioned then we can use the NGRAM Tokenizer , which will divide the single word in a different number of characters.Please find the below link to analyze your tokens.
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html
Then u can simply use a match query:
curl -XGET "http://localhost:9200/indexName/_search" -d'
{
"query": {
"match": {
"FIELDNAME": "YOURTEXT"
}
}
}'
Upvotes: 0
Reputation: 49
public function ajaxhandlerAction() {
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
switch ($method) {
case "searchgroups":
if ($this->getRequest()->isPost()) {
$name = $this->getRequest()->getPost('name');
$client = new Elasticsearch\Client();
$searchParams = array();
$searchParams['index'] = 'data';
$searchParams['type'] = 'couchbaseDocument';
$searchParams['from'] = 0;
$searchParams['size'] = 20;
if (isset($name) && !empty($name)) {
$searchParams['body']['query']['filtered']['query']['multi_match']['query'] = $name;
$searchParams['body']['query']['filtered']['query']['multi_match']['type'] = "phrase_prefix";
$searchParams['body']['query']['filtered']['query']['multi_match']['fields'] = ['name', 'city', 'state', 'country'];
$groupResults = $client->search($searchParams);
}
}
break;
default:
break;
}
}
Upvotes: 1