elasticsearch:在文本中搜索

时间:2017-05-05 10:23:11

标签: elasticsearch

例如,如果名称为P.Shanmukha Sharma,并且用户搜索Shanmukha将无法用于搜索结果。它只返回P.Shanmukha和Sharma,如果我会搜索Shanmukha并且它会返回结果有什么办法吗?

"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

2 个答案:

答案 0 :(得分:1)

所以做了很多关于ES的研究我用通配符得到了这个解决方案。谢谢EveryOne

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

答案 1 :(得分:0)

基本上,2方式可以这样做,

  1. 通过GET方法和网址:

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

  2. 通过模糊查询 由@krrish this one给出: