是时候用 ES 拯救发际线啦

Elasticsearch查询时指定分词器

Elasticsearch | 作者 elasticStack | 发布于2018年08月01日 | 阅读数:42926

问题描述:
ES目前使用的是IK分词器,查询时的分词也是IK,但现在想查询走的分词还是ES的 Standard Analyzer
es_result = get_es_connect().search(
index=index,
doc_type=_type,
body=dsl,
timeout=timeout
)
使用python语言编码, 这个search有个analyzer参数,指定为standard报错.
请问这个问题是在mapping中指定呢,还是可以在方法中指定,请指点一二?
已邀请:

kennywu76 - Wood

赞同来自: qw8613243 elasticStack Merrizee cccthought yuanzhi

有三种方式可以指定查询分析器:
1. 在mapping里指定search_analyzer,例如
PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "uid": {
          "type": "keyword"
        },
        "name": {
          "type": "text",
          "analyzer": "english",
          "search_analyzer": "standard"
        }
      }
    }
  }
}
 
2.使用URL Search的时候,指定analyzer参数 ,文档参考: https://www.elastic.co/guide/e ... .html , 对应的python代码范例: 
>>> es.search(index="my_index", analyzer="standard", q='name:"mark AND johnson"')
要注意的是,这里的analyzer只能和q这个参数搭配使用。 你的代码报错,是因为用的body参数,这个参数是没有analyzer参数搭配的。

3.使用Request Body Search,文档参考: https://www.elastic.co/guide/e ... .html  ,对应的python代码范例:
>>> dsl='{"query": {"match": {"name": {"query": "mark","analyzer": "standard"}}}}'
>>> es.search(index="my_index", body=dsl)
注意这个时候,analyzer是写到dsl里面的match query。

zyb1994111

赞同来自: elasticStack

mapping设置就好

luman

赞同来自: FuNian

查询时可以指定分词,DSL为:
{
  "query": {
    "match": {"title":{"query" : "洗衣液","analyzer" : "standard"}}
  }
}

zyb1994111

赞同来自:

就这样就可以

elasticStack - 90后it大数据男

赞同来自:

可以在mapping中指定 

PUT _template/search_analyzer
{
"template": "*",
"mappings": {
"_default_": {
"_all": {
"enabled": true
},
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer":"ik_smart",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
]
}
}
}

要回复问题请先登录注册