首页 > 解决方案 > 如何整合Mongoosastic?

问题描述

完全陌生ELASTICSEARCH

我有一个mongoose schema这样的定义

const newSchema = new mongoose.Schema({
    name: String,
    age: Number,
    interests: [Objects],
});

newSchema.plugin(mongoosastic);

New = module.exports = mongoose.model('New', newSchema);

New.createMapping({
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0,
        "analysis": {
            "filter": {
                "nGram_filter": {
                    "type": "nGram",
                    "min_gram": 2,
                    "max_gram": 20,
                    "token_chars": [
                        "letter",
                        "digit",
                        "punctuation",
                        "symbol"
                    ]
                }
            },
            "analyzer": {
                "nGram_analyzer": {
                    "type": "custom",
                    "tokenizer": "whitespace",
                    "filter": [
                        "lowercase",
                        "asciifolding",
                        "nGram_filter"
                    ]
                },
                "whitespace_analyzer": {
                    "type": "custom",
                    "tokenizer": "whitespace",
                    "filter": [
                        "lowercase",
                        "asciifolding"
                    ]
                }
            }
        }
    },
    "mappings": {
        "new": {
            "_all": {
                "analyzer": "nGram_analyzer",
                "search_analyzer": "whitespace_analyzer"
            },
            "properties": {
                "name": {
                    "type": "string",
                },
                "age": {
                    "type": "string",
                },
                "interests": {
                    "type": "string",
                },
            }
        }
    }
}, function(err, mapping) {
    if (err) {
        console.log('error');
        console.log(err);
    } else {
        console.log('mapping created!');
        console.log(mapping);
    }
});

当我运行上述项目时,我收到错误消息:

消息:'[mapper_parsing_exception] 没有在字段 [age] 上声明的类型 [string] 的处理程序'

我究竟做错了什么?我对弹性搜索完全陌生,mongoosastic。

我对elasticsearch一点都不懂,上面的createMapping功能是从网上抄来的。

标签: node.jsmongodbelasticsearchmongoosemongoosastic

解决方案


推荐阅读