Elasticsearch不敏感搜索重音

2024-04-27 13:53:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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,
    }

搜索:

^{pr2}$

我在Google,Stackoverflow和弹性体.co但我没有找到任何有效的方法。在


Tags: 方法nameindexestitledescriptionsummaryprice
1条回答
网友
1楼 · 发布于 2024-04-27 13:53:08

您需要更改查询中这些字段的映射。更改映射需要重新编制索引,以便对字段进行不同的分析,查询才能正常工作。在

基本上,你需要下面这样的东西。名为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"
            }
          }
        }
      }
    }
  }
}

还有一个问题:

^{pr2}$

另外,我强烈建议您阅读文档的这一部分:https://www.elastic.co/guide/en/elasticsearch/guide/current/asciifolding-token-filter.html

相关问题 更多 >