indexing - 如何为 ElasticSearch 设置字段映射以允许精确搜索和全文搜索?

标签 indexing lucene elasticsearch

这是我的问题:

我有一个名为 product_id 的字段,其格式类似于:

A+B-12321412

如果我使用标准文本分析器,它会将其拆分为标记,如下所示:

/_analyze/?analyzer=standard&pretty=true" -d '                                        
A+B-1232412
'
{
  "tokens" : [ {
    "token" : "a",
    "start_offset" : 1,
    "end_offset" : 2,
    "type" : "<ALPHANUM>",
    "position" : 1
  }, {
    "token" : "b",
    "start_offset" : 3,
    "end_offset" : 4,
    "type" : "<ALPHANUM>",
    "position" : 2
  }, {
    "token" : "1232412",
    "start_offset" : 5,
    "end_offset" : 12,
    "type" : "<NUM>",
    "position" : 3
  } ]
}

理想情况下,我有时希望搜索精确的产品 ID,有时则使用子字符串或仅查询部分产品 ID。

我对映射和分析器的理解是我只能为每个字段指定一个分析器。

有没有办法将字段存储为分析匹配和完全匹配?

最佳答案

是的,您可以使用 fields 参数。对于您的情况:

"product_id": {
    "type": "string",
    "fields": {
        "raw":   { "type": "string", "index": "not_analyzed" }
    }
}

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_multi_fields.html

这允许您使用两种不同的定义对相同的数据进行两次索引。在这种情况下,它将通过默认分析器和 not_analyzed 进行索引,后者只会获取精确匹配。这对于对返回结果进行排序也很有用:

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/multi-fields.html

但是,您需要花一些时间考虑如何进行搜索。特别是,给定的零件编号混合有字母、数字和标点符号或特殊字符,您可能需要发挥创意来调整查询和匹配。

关于indexing - 如何为 ElasticSearch 设置字段映射以允许精确搜索和全文搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25069321/

相关文章:

sql-server - SQL Server中主键(簇)和簇唯一索引的区别

java - 如何在solrj中使用Solr的所有核心

lucene - 如何删除Lucene索引而不影响目录中其他非索引文件?

elasticsearch - 将反向 token 与自定义规范化器一起使用

javascript - 使用 Javascript 按索引获取 HTML 有序列表中的元素

mysql - 同一表中引用的索引

lucene - 谁能为Lucene建议一些好的教程?

mysql - RDBMS 到 Elasticsearch

elasticsearch - 嵌套字段值的聚集Elasticsearch索引

database - 根据前缀在表中查找单词