2017-05-05 132 views
0

例如,如果名稱爲P.Shanmukha Sharma,並且用戶搜索Shanmukha將無法用於搜索結果。它只返回P.Shanmukha和Sharma,有沒有什麼方法可以搜索Shanmukha並返回結果?elasticsearch:在文本內搜索

"user" : { 
     "properties" : { 
      "city" : { 
      "type" : "string", 
      "analyzer" : "autocomplete", 
      "search_analyzer" : "standard" 
      }, 
      "created" : { 
      "type" : "date", 
      "format" : "strict_date_optional_time||epoch_millis" 
      }, 
      "id" : { 
      "type" : "long" 
      }, 
      "latitude" : { 
      "type" : "double" 
      }, 
      "longitude" : { 
      "type" : "double" 
      }, 
      "profile_image" : { 
      "type" : "string" 
      }, 
      "state" : { 
      "type" : "string", 
      "analyzer" : "autocomplete", 
      "search_analyzer" : "standard" 
      }, 
      "super_verification" : { 
      "type" : "string" 
      }, 
      "type" : { 
      "type" : "string" 
      }, 
      "username" : { 
      "type" : "string", 
      "analyzer" : "autocomplete", 
      "search_analyzer" : "standard" 
      } 
     } 
     } 

用戶名被定義爲一個搜索分析器

和搜索查詢是

def EsSearch(self, index, page, size, searchTerm): 
    body = { 
     'query': { 
      'match': searchTerm 
     }, 
     'sort': { 
       'created': { 
        'order': 'desc' 
       } 
     }, 
     'filter': { 
       'term': { 
        'super_verification': 'verified' 
       } 
     } 
    } 
    res = self.conn.search(index=index, body=body) 
    output = [] 
    for doc in res['hits']['hits']: 
     output.append(doc['_source']) 
    return output 
+0

您正在使用哪個版本的elasticSearch? –

+0

我的版本是2.3.2 –

+0

請提供您正在使用的搜索查詢 –

回答

1

這樣做了這麼多的研究ES我有通配符這一解決方案。謝謝EveryOne

{ 
"query": { 
    "wildcard": { 
     "username": { 
      "value": "*Shanmukha*" 
     } 
    } 
} 
} 
0

基本上,在2的方式來這樣做,

  1. 通過GET方法和URL :

    http://localhost:9200/your_index/your_type/_search?q=username:*Shanmukha*&pretty=true

  2. 通過模糊查詢 通過@krrish this one給出:

+0

但我需要在整個索引中搜索而不是特定類型。因爲模糊不存在捲曲。這個查詢我正在使用,並得到一個錯誤,如[匹配]查詢不支持[用戶]。 [體] =>數組 ( [查詢] =>數組 ( [匹配] =>數組 ( [模糊] =>數組 ( [用戶] => IVA ) ) ) –

+0

如果我會去得到方法和URL如何將我的搜索查詢.. ?? –