首页 > 解决方案 > Elasticsearch - 如何为搜索和索引指定相同的分析器

问题描述

我正在开发一个西班牙搜索引擎。(我不会说西班牙语)但根据我的研究,目标或多或少是这样的: 1. 过滤停用词,如“dos”、“de”、“la”... 2. 为两个搜索词干和索引。例如,如果您搜索“primera”,那么“primero”、“primer”也应该出现。

我的尝试:

es_analyzer={
        "settings": {
            "analysis": {
            "filter": {
                "spanish_stop": {
                "type":       "stop",
                "stopwords":  "_spanish_" 
                },
                "spanish_stemmer": {
                "type":       "stemmer",
                "language":   "spanish"
                }
            },
            "analyzer": {
                "default_search": {
                    "type": "spanish"
                },
                "rebuilt_spanish": {
                "tokenizer":  "standard",
                "filter": [
                    "lowercase",
                    "spanish_stop",
                    "spanish_stemmer"
                ]
                }
            }
            }
        }
    }

问题:当我在 中使用"type":"spanish""default_search",我的查询“primera”被提取为“primer”,这是正确的,但即使我指定"spanish_stemmer"在过滤器中使用,索引中的文档也不会被提取。因此,当我搜索“primera”时,它只显示“primer”的完全匹配。有关解决此问题的任何建议吗?

潜在的修复,但我还没有弄清楚语法:

  1. 在过滤器中使用内置"spanish"分析器。语法是什么?
  2. "default_search". 但我不知道如何在那里使用复合设置。

标签: elasticsearchelasticsearch-analyzers

解决方案


添加具有索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

 {
  "settings": {
    "analysis": {
      "filter": {
        "spanish_stop": {
          "type": "stop",
          "stopwords": "_spanish_"
        },
        "spanish_stemmer": {
          "type": "stemmer",
          "language": "spanish"
        }
      },
      "analyzer": {
        "default_search": {
          "type":"spanish",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "spanish_stop",
            "spanish_stemmer"
          ]
        }
      }
    }
  },
  "mappings":{
    "properties":{
      "title":{
        "type":"text",
        "analyzer":"default_search"
      }
    }
  }
}

指数数据:

{
  "title": "primer"
}
{
  "title": "primera"
}
{
  "title": "primero"
}

搜索查询:

{
  "query":{
    "match":{
      "title":"primer"
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64420517",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.13353139,
        "_source": {
          "title": "primer"
        }
      },
      {
        "_index": "stof_64420517",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.13353139,
        "_source": {
          "title": "primera"
        }
      },
      {
        "_index": "stof_64420517",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.13353139,
        "_source": {
          "title": "primero"
        }
      }
    ]

推荐阅读