必须指定分析器类型或令牌生成器

时间:2019-05-28 14:35:25

标签: elasticsearch

我基本上是弹性搜索的新手。我正在尝试在“ name_auto”字段上实现模糊搜索,同义词搜索,边缘ngram和自动完成,但是看来我的索引创建失败了。
另一个问题是,如果可以的话,我可以为“名称”字段实现所有分析器吗?

  {
        "settings": {
            "index": {
                "analysis": {
                    "filter": {
                        "synonym": {
                            "ignore_case": "true",
                            "type": "synonym",
                            "format": "wordnet",
                            "synonyms_path": "analysis/wn_s.pl"
                        }
                    },
                    "analyzer": {
                        "synonym": {
                            "tokenizer": "whitespace",

                            "filter": [
                                "synonym"
                            ]
                        },

                        "keyword_analyzer": {
                            "filter": [
                                "lowercase",
                                "asciifolding",
                                "trim"
                            ],
                            "char_filter": [],
                            "type": "custom",
                            "tokenizer": "keyword"
                        },
                        "edge_ngram_analyzer": {
                            "filter": [
                                "lowercase"
                            ],
                            "tokenizer": "edge_ngram_tokenizer"
                        },
                        "edge_ngram_search_analyzer": {
                            "tokenizer": "lowercase"
                        },


                        "tokenizer": {
                            "edge_ngram_tokenizer": {
                                "type": "edge_ngram",
                                "min_gram": 1,
                                "max_gram": 25,
                                "token_chars": [
                                    "letter"
                                ]
                            }
                        }
                    },
                    "mappings": {
                        "properties": {
                            "firebaseId": {
                                "type": "text"
                            },
                            "name": {
                                "fielddata": true,
                                "type": "text",
                                "analyzer": "standard"
                            },
                            "name_auto": {
                                "type": "text",

                                "fields": {
                                    "keywordstring": {
                                        "type": "text",
                                        "analyzer": "keyword_analyzer"
                                    },
                                    "edgengram": {
                                        "type": "text",
                                        "analyzer": "edge_ngram_analyzer",
                                        "search_analyzer": "edge_ngram_search_analyzer"
                                    },
                                    "completion": {
                                        "type": "completion"
                                    },
                                    "synonym_analyzer": {
                                        "type": "synonym",
                                        "analyzer": "synonym"
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
    }

这是输出:

>   {
>         "error": {
>             "root_cause": [
>                 {
>                     "type": "illegal_argument_exception",
>                     "reason": "analyzer [tokenizer] must specify either an analyzer type, or a tokenizer"
>                 }
>             ],
>             "type": "illegal_argument_exception",
>             "reason": "analyzer [tokenizer] must specify either an analyzer type, or a tokenizer"
>         },
>         "status": 400
>     }

我在哪里做错了,请指导我正确的方向。

1 个答案:

答案 0 :(得分:1)

您的tokenizer部分位于analyzer部分的内部,这是不正确的。试试这个,它应该可以工作:

{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym": {
            "ignore_case": "true",
            "type": "synonym",
            "format": "wordnet",
            "synonyms_path": "analysis/wn_s.pl"
          }
        },
        "analyzer": {
          "synonym": {
            "tokenizer": "whitespace",
            "filter": [
              "synonym"
            ]
          },
          "keyword_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding",
              "trim"
            ],
            "char_filter": [],
            "type": "custom",
            "tokenizer": "keyword"
          },
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "edge_ngram_search_analyzer": {
            "tokenizer": "lowercase"
          }
        },
        "tokenizer": {
          "edge_ngram_tokenizer": {
            "type": "edge_ngram",
            "min_gram": 1,
            "max_gram": 25,
            "token_chars": [
              "letter"
            ]
          }
        }
      },
      "mappings": {
        "properties": {
          "firebaseId": {
            "type": "text"
          },
          "name": {
            "fielddata": true,
            "type": "text",
            "analyzer": "standard"
          },
          "name_auto": {
            "type": "text",
            "fields": {
              "keywordstring": {
                "type": "text",
                "analyzer": "keyword_analyzer"
              },
              "edgengram": {
                "type": "text",
                "analyzer": "edge_ngram_analyzer",
                "search_analyzer": "edge_ngram_search_analyzer"
              },
              "completion": {
                "type": "completion"
              },
              "synonym_analyzer": {
                "type": "synonym",
                "analyzer": "synonym"
              }
            }
          }
        }
      }
    }
  }
}