我正在尝试用创建一个自动完成,到目前为止,我已经能够使用sense创建它,但是我很难将它移植到mongoosastic。
我学习了this文档中的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"
}
}
}但是,我很难将它移植到蒙哥拉斯。我尝试了以下方法:
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端点,这就是为什么”故事-建议“查询工作的原因。但是,在使用mongoosastic时,我被限制使用.search({})来查询我认为类似于_search的查询。然而,我似乎无法找到一种方法来完成我正在寻找的自动完成的_suggest行为,而且当我尝试使用建议进行查询时,我总是会在ElasticSearch中得到解析错误。
有没有一种方法来完成我想要做的事情,要么用单弹性搜索,要么用弹性搜索?
我尝试过使用"sense“来完成这个任务,但是即使我得到了关于”自动完成“的建议,我也得到了大量的SearchParseExceptions:
GET _search
{
"query": {
"match": { title : "bow" }
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}发布于 2015-09-30 05:16:37
以下是基本自动完成的完整解决方案:
suggest查询,该查询使用我们在模式中创建的"es_completion“字段;2) size = 0,以使空体查询不返回任何标记。
Tag.search(空,{建议:{“标签-建议”:{“aTermToAutocomplete”:"aTermToAutocomplete",“完成”:{“字段”:“名称”} },“大小”:0 },函数(err,结果){ if (err) {返回console.log(JSON.stringify(err,null,4));}返回console.log(JSON.stringify(结果,null,4);});发布于 2015-09-25 14:20:34
我找到了一条路。使用mongoosastic的最新更新,您可以这样做:
GET _search
{
"query": {
"match_all": {}
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}这是因为新的蒙哥拉斯版本支持建议者。只需注意您正在做什么,特别是在查询部分,因为"match_all“将匹配所有文档并给出不必要的大响应;但是,无论您在建议者的”文本“部分中放置了什么,都会将单词返回给自动完成。
还请注意,为了使其工作,您需要在模型定义中使用createMapping方法,如下所示:
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);
var Story = mongoose.model('Story', StorySchema);
Story.createMapping({}, function(err, mapping){
// ...
});https://stackoverflow.com/questions/32461260
复制相似问题