1

我正在使用ElasticSearch 的完成建议器在数据库中查找标签。它有效,但我想更改为termSuggester。因为有些标签有两个词,比如“abc def”,如果我搜索“abc”,我可以找到它,但我不能用“def”。因为完成建议器仅适用于前缀搜索。

我如何使用术语建议器?我在 mongoosastic 的文档中一无所获。

这是我使用完成建议的代码:

// create tag model
var mongoose     = require('mongoose');
var mongoosastic = require('mongoosastic');

var tagSchema = mongoose.Schema({

    name: {
        type: String,
        es_indexed: true,
        es_analyzer: 'simple',
        es_payloads: true,
        es_type: 'completion',
    }

});

tagSchema.plugin(mongoosastic);

var Tag = mongoose.model('Tag', tagSchema);

// add some data
Tag.createMapping(function(err, mapping) {
    async.each(tags, function(t, done) {
        var newTag = new Tag(t);
        newTag.save(function(err) {
            if (err) { console.log(err); }
            newTag.on('es-indexed', function(err, res) {
                if (err) { console.log(err); }
                done();
            });
        });
    });
});

// looking for some tags with keyword
Tag.search(
    null,
    {
        suggest: {
            "tags-suggest": {
                "text": keyword,
                "term": {
                    "field": "name",
                    "size": 10
                }
            }
        },
        size: 0
    },
    function(err, data) {
        console.log(data);
    }
)

谢谢 !

4

0 回答 0