【发布时间】:2017-02-01 03:12:09
【问题描述】:
我想用 mongoose 在弹性搜索中创建索引,表达但没有可用的文档。我尝试了 mongoosastic,但感觉不舒服。
那么有人可以帮助我吗?
【问题讨论】:
标签: javascript node.js mongodb express mongoose
我想用 mongoose 在弹性搜索中创建索引,表达但没有可用的文档。我尝试了 mongoosastic,但感觉不舒服。
那么有人可以帮助我吗?
【问题讨论】:
标签: javascript node.js mongodb express mongoose
我已经找到了一个解决拼写错误搜索和基于附近搜索全文的解决方案。首先我将定义拼写错误单词的模糊搜索。
var searchParams = {
index: 'product',
body: {
"query": {
"query_string": {
//search keyword is string which is searched
"query": searchKeyword+'~',
}
}
}
};
esClient.search(searchParams,function(err, response){
})
另一个搜索更多类似的......
"more_like_this": {
"fields": [
"productName","description","brand"
],
//searchKeyword is a string variable for searching
"like_text": searchKeyword,
"min_term_freq": 1,
"min_doc_freq": 1,
"max_query_terms": 12
}
}
}
如果你想模糊结果,那么也试试这个
var searchParams = {
index: 'product',
body: {
query:{
"fuzzy" : {
"ProductName" : {
"value" : searchKeyword,
"boost" : 1.0,
"fuzziness" : 3,
"prefix_length" : 0,
"max_expansions": 100
}
}
}
}
}
【讨论】:
这对索引很有效。所以我添加了搜索查询。我的搜索查询是这样的
esClient.search({
'index': 'product',
//'q': '*'+searchKeyword+'*' this is searching query
'body': {
'query': {
'prefix': {'_all': searchKeyword}
}
}
},function(err, response){
})
但是有一个问题。它只搜索基于文本的搜索,没有拼写错误,自动更正,附近,类似等。我想搜索被检查拼写错误,类似,附近所有结果的查询。所以请有人帮助我. 谢谢。
【讨论】:
你可以使用这个模块
https://github.com/elastic/elasticsearch-js
它使用起来非常简单,并且有很多文档。
只需连接到 DB-> 获取您需要的记录-> 为每个记录运行发布(client.bulk 方法)。
https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html
编辑 这是一个例子
var es = require('elasticsearch');
var client = new es.Client({
host: 'localhost:9200',
log: 'error'
});
//doc is the mongoDB mocument
var bulkData = [{index: {_index: "yourIndexName", _type: "Any type", _id: doc._id}}, doc];
client.bulk({
requestTimeout: 300000,
body: bulkData
}, function(err, response){//final callback here});
希望这会有所帮助。
【讨论】: