如何在弹性搜索中使用like运算符?

时间:2016-03-10 12:39:42

标签: php elasticsearch

如何在elasticsearch中搜索数据。我的意思是如果我在文本字段中推送任何数据,那么应该搜索和显示该单词的相关数据。     我尝试过:

$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
} 

2 个答案:

答案 0 :(得分:1)

    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;
}
 }

答案 1 :(得分:0)

如果你需要像你提到的那样进行搜索,那么我们就可以使用NGRAM Tokenizer,它将单个单词分成不同数量的字符。请找到以下链接来分析你的标记。

https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html

然后你可以简单地使用匹配查询:

curl -XGET "http://localhost:9200/indexName/_search" -d'
{
   "query": {
      "match": {
         "FIELDNAME": "YOURTEXT"
      }
   }
}'