python - Elasticsearch 不敏感搜索重音

标签 python elasticsearch elasticsearch-2.0

我正在将 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/

相关文章:

python - TypeError: 'function' 对象不可下标 python。我正在尝试将每个 .mp4 文件合并为一个视频

python - 从图像中读取文本

ruby-on-rails - Elasticsearch为法语或英语字段配置词干

python - 使用 np.unique 从 2 个 numpy 数组中删除成对重复项

python - 通过 kwargs 更新模型 django

c# - 使用 NEST 客户端获取 Elasticsearch 类型映射名称

Elasticsearch:影响评分与文档中的自定义评分字段

elasticsearch - Elasticsearch多数据路径磁盘已满

elasticsearch - 无法在同一台计算机上运行两个单独版本的Elasticsearch

node.js - 具有过滤器和聚合的 Elastic Search 查询不返回聚合