使用elasticsearch

时间:2016-05-26 23:18:18

标签: elasticsearch mapping stop-words

我正在尝试为我的映射添加一个停用词列表,但是我收到了一个错误。这是映射:

PUT test-recipe
{
  "mappings": {
    "recipe" : {
      "properties" : {
        "ingredients" : {
          "type" :    "string",
          "analyzer": "english",
          "stopwords": ["my", "stop", "words"]
        }
      }
    }
  }
}

此映射在没有停用词参数的情况下正常工作。但是使用停用词字段,我得到以下错误:

{
   "error": {
      "root_cause": [
         {
            "type": "mapper_parsing_exception",
            "reason": "Mapping definition for [ingredients] has unsupported parameters:  [stopwords : [Best®, Halves, organic, island, free, gluten, high-gluten, segments, baking, cooking, new, active, dry, leaves, slices, sliced, warm, root, hot, jack, extract, slivered, sliver, non-fat, fat, chopped, skinless, seed, nonfat, melted, cracked, in, split, vegetable, smoked, medium, nectar, all-purpose, fraîche, fresh]]"
         }
      ],
      "type": "mapper_parsing_exception",
      "reason": "Failed to parse mapping [recipe]: Mapping definition for [ingredients] has unsupported parameters:  [stopwords : [Best®, Halves, organic, island, free, gluten, high-gluten, segments, baking, cooking, new, active, dry, leaves, slices, sliced, warm, root, hot, jack, extract, slivered, sliver, non-fat, fat, chopped, skinless, seed, nonfat, melted, cracked, in, split, vegetable, smoked, medium, nectar, all-purpose, fraîche, fresh]]",
      "caused_by": {
         "type": "mapper_parsing_exception",
         "reason": "Mapping definition for [ingredients] has unsupported parameters:  [stopwords : [Best®, Halves, organic, island, free, gluten, high-gluten, segments, baking, cooking, new, active, dry, leaves, slices, sliced, warm, root, hot, jack, extract, slivered, sliver, non-fat, fat, chopped, skinless, seed, nonfat, melted, cracked, in, split, vegetable, smoked, medium, nectar, all-purpose, fraîche, fresh]]"
      }
   },
   "status": 400
}

如果你能告诉我为什么会遇到这个问题,这将是我的一天。此外,在做“更喜欢这个查询”时,停用词列表会考虑到吗?

1 个答案:

答案 0 :(得分:1)

您必须为停用词创建过滤器并在分析器中使用

#remove index
#DELETE recipe

#put mapping, analyzer and filter for stop words
PUT recipe
{
  "settings": {
    "analysis": {
      "analyzer": {
        "cooking_nonstop": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "english_morphology",
            "my_stopwords"
          ]
        }
        },
        "filter": {
          "my_stopwords": {
            "type": "stop",
            "stopwords": "Best®,Halves,organic,island,free,gluten,high-gluten,segments,baking,cooking,new,active,dry,leaves,slices,sliced,warm,root,hot,jack,extract,slivered,sliver,non-fat,fat,chopped,skinless,seed,nonfat,melted,cracked,in,split,vegetable,smoked,medium,nectar,all-purpose,fraîche,fresh"
          }
        }
      }
    },
    "mappings": {
      "recipe": {
        "properties": {
          "ingredients": {
            "type": "string",
            "analyzer": "cooking_nonstop"
          }
        }
      }
    }
  }

#check analyzer
GET /recipe/_analyze?analyzer=cooking_nonstop&text=put+fresh+egs+in+hot+water

#create document
POST recipe/recipe/boiled_egs
{
  "ingredients":"put fresh egs in hot water"
}

#another stop word filter demonstration
POST recipe/_search
{
  "aggs": {
    "terms": {
      "terms": {
        "field": "ingredients",
        "size": 10
      }
    }
  }
}