我正在将 Elastic 搜索与 Python 结合使用。我找不到用重音符号进行不敏感搜索的方法。
例如: 我有两个字。 “卡米翁”和“卡米恩”。 当用户搜索“camion”时,我希望显示两个结果。
创建索引:
es = Elasticsearch([{u'host': u'127.0.0.1', u'port': b'9200'}])
es.indices.create(index='name', ignore=400)
es.index(
index="name",
doc_type="producto",
id=p.pk,
body={
'title': p.titulo,
'slug': p.slug,
'summary': p.summary,
'description': p.description,
'image': foto,
'price': p.price,
'wholesale_price': p.wholesale_price,
'reference': p.reference,
'ean13': p.ean13,
'rating': p.rating,
'quantity': p.quantity,
'discount': p.discount,
'sales': p.sales,
'active': p.active,
'encilleria': p.encilleria,
'brand': marca,
'brand_title': marca_titulo,
'sellos': sellos_str,
'certificados': certificados_str,
'attr_naturales': attr_naturales_str,
'soluciones': soluciones_str,
'categories': categories_str,
'delivery': p.delivery,
'stock': p.stock,
'consejos': p.consejos,
'ingredientes': p.ingredientes,
'es_pack': p.es_pack,
'temp': p.temp,
'relevancia': p.relevancia,
'descontinuado': p.descontinuado,
}
搜索:
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': '127.0.0.1', 'port': '9200'}])
resul = es.search(
index="name",
body={
"query": {
"query_string": {
"query": "(title:" + search + " OR description:" + search + " OR summary:" + search + ") AND (active:true)",
"analyze_wildcard": False
}
},
"size": "9999",
}
)
print resul
我在 Google、Stackoverflow 和 elastic.co 上进行了搜索,但没有找到任何有效的内容。
最佳答案
您需要更改查询中这些字段的映射。更改映射需要重新索引,以便以不同的方式分析字段并使查询正常工作。
基本上,您需要如下所示的内容。名为 text
的字段只是一个示例。您还需要对其他字段应用相同的设置。请注意,我在其中使用了 fields
,以便根字段将保留默认情况下分析的原始文本,而 text.folded
将删除重音字符,并使您的查询起作用。我还对查询进行了一些更改,以便您搜索该字段的两个版本(camion
将匹配,但也会匹配 camión
)。
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"text": {
"type": "string",
"fields": {
"folded": {
"type": "string",
"analyzer": "folding"
}
}
}
}
}
}
}
以及查询:
"query": {
"query_string": {
"query": "\\*.folded:camion"
}
}
此外,我强烈建议阅读文档的这一部分:https://www.elastic.co/guide/en/elasticsearch/guide/current/asciifolding-token-filter.html
关于python - Elasticsearch 不敏感搜索重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38452791/