使用 mongoosastic 进行自动完成
Use mongoosastic for an autocomplete
我正在尝试使用 mongoosastic 和 Elastic Search 创建一个自动完成功能,到目前为止,我已经能够使用 sense 创建它,但是我在将它移植到 mongoosastic 时遇到了问题。
我遵循了 ElasticSearch 文档中的 this 教程,并且能够使用 "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" 执行此操作,但即使我得到了 "autocomplete" 的建议,我也得到了一堆 SearchParseExceptions:
GET _search
{
"query": {
"match": { title : "bow" }
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}
我找到方法了。使用 mongoosastic 库的最新更新,你可以做这样的事情:
GET _search
{
"query": {
"match_all": {}
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}
这是因为新的 mongoosastic 版本支持 suggesters。请注意您在做什么,特别是在查询部分,因为 "match_all" 会匹配所有文档并给您一个不必要的大响应;但是无论您在建议者的 "text" 部分中放置什么,都会 return 自动完成这些词。
另请注意,为了使其正常工作,您需要在模型定义中使用 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){
// ...
});
这是基本自动完成的完整解决方案:
将必要的参数添加到您的架构中:
var TagSchema = new Schema({
name: {
type: String,
unique: true,
required: true,
es_type: 'completion',
es_index_analyzer: 'simple',
es_search_analyzer: 'simple',
es_payloads: true
}
});
将插件添加到您的模式并创建模型:
TagSchema.plugin(mongoosastic);
var Tag = mongoose.model('Tag', TagSchema);
使用create mapping方法向ES注册映射。如果您不执行此步骤,您的索引将在创建第一个文档并编制索引时使用默认值注册:
Tag.createMapping(function(err, mapping) {
if (err) {
console.log('error creating mapping (you can safely ignore this)');
console.log(err);
} else {
console.log('mapping created!');
console.log(mapping);
}
});
*可选 - 索引数据库中的现有文档:
var stream = Tag.synchronize(),
count = 0;
stream.on('data', function(err, doc) {
count++;
});
stream.on('close', function() {
console.log('indexed ' + count + ' documents!');
});
stream.on('error', function(err) {
console.log(err);
});
使用空查询主体进行搜索并提供两个选项 - 1) 使用 ES suggest
查询,它使用我们在模式中创建的 "es_completion" 字段; 2) size = 0
这样空体查询就不会返回标签。
Tag.search(null, {
suggest: {
"tag-suggest": {
"text": "aTermToAutocomplete",
"completion": {
"field": "name"
}
}
},
"size" : 0
},
function(err, results) {
if (err) {
return console.log(JSON.stringify(err, null, 4));
}
return console.log(JSON.stringify(results, null, 4));
});
我正在尝试使用 mongoosastic 和 Elastic Search 创建一个自动完成功能,到目前为止,我已经能够使用 sense 创建它,但是我在将它移植到 mongoosastic 时遇到了问题。
我遵循了 ElasticSearch 文档中的 this 教程,并且能够使用 "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" 执行此操作,但即使我得到了 "autocomplete" 的建议,我也得到了一堆 SearchParseExceptions:
GET _search
{
"query": {
"match": { title : "bow" }
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}
我找到方法了。使用 mongoosastic 库的最新更新,你可以做这样的事情:
GET _search
{
"query": {
"match_all": {}
},
"suggest": {
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
}
这是因为新的 mongoosastic 版本支持 suggesters。请注意您在做什么,特别是在查询部分,因为 "match_all" 会匹配所有文档并给您一个不必要的大响应;但是无论您在建议者的 "text" 部分中放置什么,都会 return 自动完成这些词。
另请注意,为了使其正常工作,您需要在模型定义中使用 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){
// ...
});
这是基本自动完成的完整解决方案:
将必要的参数添加到您的架构中:
var TagSchema = new Schema({ name: { type: String, unique: true, required: true, es_type: 'completion', es_index_analyzer: 'simple', es_search_analyzer: 'simple', es_payloads: true } });
将插件添加到您的模式并创建模型:
TagSchema.plugin(mongoosastic); var Tag = mongoose.model('Tag', TagSchema);
使用create mapping方法向ES注册映射。如果您不执行此步骤,您的索引将在创建第一个文档并编制索引时使用默认值注册:
Tag.createMapping(function(err, mapping) { if (err) { console.log('error creating mapping (you can safely ignore this)'); console.log(err); } else { console.log('mapping created!'); console.log(mapping); } });
*可选 - 索引数据库中的现有文档:
var stream = Tag.synchronize(), count = 0; stream.on('data', function(err, doc) { count++; }); stream.on('close', function() { console.log('indexed ' + count + ' documents!'); }); stream.on('error', function(err) { console.log(err); });
使用空查询主体进行搜索并提供两个选项 - 1) 使用 ES
suggest
查询,它使用我们在模式中创建的 "es_completion" 字段; 2)size = 0
这样空体查询就不会返回标签。Tag.search(null, { suggest: { "tag-suggest": { "text": "aTermToAutocomplete", "completion": { "field": "name" } } }, "size" : 0 }, function(err, results) { if (err) { return console.log(JSON.stringify(err, null, 4)); } return console.log(JSON.stringify(results, null, 4)); });