我正在尝试使用 mongoosastic 和 Elastic Search 创建一个自动完成功能,到目前为止,我已经能够使用 sense 创建它,但我无法将它移植到 mongoosastic。
我按照ElasticSearch 文档中的本教程进行操作,并且能够使用“sense”和如下所示的映射来实现我想要的:
PUT storys/story/_mapping
{
"story" : {
"properties": {
"description": {
"type": "string"
},
"title": {
"type" : "completion",
"index_analyzer": "simple",
"search_analyzer": "simple"
}
}
}
}
和这样的查询:
GET storys/_suggest
{
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
但是,我在将其移植到 mongoosastic 时遇到了麻烦。我尝试了以下方法:
var StorySchema = new Schema({
title:{
type: String, es_type:'completion', es_index_analyzer: 'simple', es_search_analyzer: 'simple', es_payloads: true
},
description: {
type: String
}
});
StorySchema.plugin(mongoosastic);
从服务器控制器查询时:
Story.search({
query: {
"match": { title : req.query.q }
},
suggest: {
"my-title-suggestions-1" :{
text: req.query.q,
completion: {
field: 'title'
}
}
}
});
我知道当我使用“sense”时,我使用的是 _suggest 端点,这就是“story-suggest”查询有效的原因。但是,在使用 mongoosastic 时,我仅限于使用 .search({}) 进行查询,我想这就像 _search 一样。但是,我似乎无法找到一种方法来完成我正在寻求自动完成的 _suggest 行为,并且当我尝试使用建议进行查询时,我一直在 ElasticSearch 中遇到解析错误。
有没有办法通过 mongoosastic 或弹性搜索来完成我想要做的事情?
我曾尝试使用“sense”来执行此操作,但即使我得到了“自动完成”的建议,我也得到了一堆 SearchParseExceptions:
GET _search
{
"query": {
"match": { title : "bow" }
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}