【问题标题】:Elasticsearch insensitive search accentsElasticsearch 不敏感的搜索口音
【发布时间】:2016-07-19 16:24:50
【问题描述】:

我在 Python 中使用弹性搜索。我找不到用重音进行不敏感搜索的方法。

例如: 我有两个字。 “Camion”和“Camion”。 当用户搜索“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 上进行了搜索,但没有找到任何可行的方法。

【问题讨论】:

  • 您在查询中使用的那些字段的映射是什么?
  • 你的意思是在数据库中?所有字符串。我必须在查询中声明任何内容吗?
  • 什么数据库? :-)
  • 对不起,我是弹性新手。我的意思是指数。我已经用我的所有代码更新了这个问题。 =)

标签: python elasticsearch elasticsearch-2.0


【解决方案1】:

您需要更改查询中的那些字段的映射。更改映射需要重新索引,以便对字段进行不同的分析并且查询将起作用。

基本上,您需要以下内容。名为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

【讨论】:

  • 我见过这样的事情。但是我把这段代码放在哪里呢?在 es.index() 上的 body{} 之前?
  • 我不懂 Python。对不起。我提供的代码使用这些设置和映射创建索引。因此,需要删除现有索引,我提供的用于创建新索引的代码,需要重新索引数据。
  • @AndreiStefan 谢谢,这在 ES 7.14 中似乎仍然有效,但文档已经过时了。
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 2017-08-21
相关资源
最近更新 更多