简体   繁体   English

在弹性搜索中如何使用像算子?

[英]how to use like operator in elastic search?

How can I search a data in elasticsearch. 如何在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
} 
    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;
}
 }

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. 如果您需要像您提到的内容一样进行搜索,那么我们可以使用NGRAM Tokenizer,它将单个单词划分为不同数量的字符。请找到以下链接以分析您的令牌。

https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html https://www.elastic.co/guide/zh-CN/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"
      }
   }
}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM