语言分析器无法正常工作,无法找到单个结果

时间:2019-04-06 14:33:37

标签: elasticsearch analyzer

我的“类别”字段中有一堆带有翻译的类别。我已经为索引中的字段定义了语言分析器,以便可以搜索它们。但是它找不到我单词的单数形式。 wasmachine中的titles.title-nlwasmachines的单数,但未找到。我想念什么?

演示文档

    "_source" : {
      "google_id" : 2706,
      "titles" : [
        {
          "title-en" : "laundry appliances",
          "title-de" : "waschen & trocknen",
          "title-fr" : "appareils de blanchisserie",
          "title-nl" : "wasmachines"
        }
      ]
    }

我如何映射它们

PUT categories/_mapping/category
{
"dynamic": false,
"properties": {
"titles.title-nl": {
"type": "text",
"analyzer": "dutch"
},
"titles.title-en": {
    "type": "text",
      "analyzer": "english"
    },
    "titles.title-de": {
    "type": "text",
      "analyzer": "german"
    },
    "titles.title-fr": {
    "type": "text",
      "analyzer": "french"
    }
  }
}

我搜索它们的方式

GET categories/_search
{
  "size": 4, 
  "query": {
    "multi_match": {
      "query": "wasmachines",
      "fields": ["titles.title-de","titles.title-en", "titles.title-fr", "titles.title-nl"]
    }
  }
}

1 个答案:

答案 0 :(得分:2)

问题在于默认的荷兰分析器不知道如何词干wasmachines,您将需要使用stemmer_override使用自定义分析器来重新创建索引。

docs中,您可以执行以下操作来重新创建dutch分析器,并告诉我们wasmachines应该是wasmachine的缩写,只需放置wasmachine => wasmachinesstemmer_override

的规则之内
PUT categories/
{
  "settings": {
    "analysis": {
      "filter": {
        "dutch_stop": {
          "type":       "stop",
          "stopwords":  "_dutch_" 
        },
        "dutch_keywords": {
          "type":       "keyword_marker",
          "keywords":   ["voorbeeld"] 
        },
        "dutch_stemmer": {
          "type":       "stemmer",
          "language":   "dutch"
        },
        "dutch_override": {
          "type":       "stemmer_override",
          "rules": [
            "fiets=>fiets",
            "bromfiets=>bromfiets",
            "wasmachine=>wasmachines",
            "ei=>eier",
            "kind=>kinder"
          ]
        }
      },
      "analyzer": {
        "rebuilt_dutch": {
          "tokenizer":  "standard",
          "filter": [
            "lowercase",
            "dutch_stop",
            "dutch_keywords",
            "dutch_override",
            "dutch_stemmer"
          ]
        }
      }
    }
  }
}

您还需要在映射中使用该新分析器:

PUT categories/_mapping/category
{
    "dynamic": false,
    "properties": {
        "titles.title-nl": {
            "type": "text",
            "analyzer": "rebuilt_dutch"
        },
        "titles.title-en": {
            "type": "text",
            "analyzer": "english"
        },
        "titles.title-de": {
            "type": "text",
            "analyzer": "german"
        },
        "titles.title-fr": {
            "type": "text",
            "analyzer": "french"
        }
    } 
}

此后,您将可以搜索wasmachine并获取包含wasmachines的文档。