3

在 Elasticsearch 2.x 中,我如何区分首字母缩略词“CAN”和常见的英文单词“can”,同时仍在我的分析器中使用“小写”过滤器(用于搜索不区分大小写)?

我正在使用的自定义分析器是:

"analyzer": {
    "tight": {
        "type": "custom",
        "tokenizer": "standard",
        "stopwords": "_english_",
        "filter": ["lowercase", "asciifolding"]
    }
}

在索引时,当大写首字母缩写词“CAN”命中我的分析器时,它变成了英文单词“can”。然后,当我搜索“CAN”时,我会得到所有包含英文单词“can”的文档。我只想要包含大写单词“CAN”的文档。可能还有其他首字母缩略词属于类似的模式。

解决这个问题的最佳方法是什么?

4

1 回答 1

1

实现它的一种方法是创建另一个没有lowercase标记过滤器的分析器,并在主字段的子字段上使用该分析器。它是这样的:

使用两个分析器tighttight_acronym. 前者分配给 the field,后者分配给field.acronyms子字段:

PUT index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "tight": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        },
        "tight_acronym": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "field": {
          "type": "string",
          "analyzer": "tight",
          "fields": {
            "acronyms": {
              "type": "string",
              "analyzer": "tight_acronym"
            }
          }
        }
      }
    }
  }
}

然后我们索引两个文档:

PUT index/test/1
{ "field": "It is worth CAN 300" }
PUT index/test/2
{ "field": "can you do it?" }

然后,如果您搜索CAN(在子字段上),您将获得第一个文档

POST index/test/_search
{
  "query": {
    "match": {
      "field.acronyms": "CAN"
    }
  }
}

如果您搜索can(在主字段上),您将获得第二个文档

POST index/test/_search
{
  "query": {
    "match": {
      "field": "can"
    }
  }
}
于 2016-08-15T05:52:12.377 回答