我的目标是在mongoosastic搜索中填充某些字段,但如果我执行以下代码,它将始终返回
这是代码
var mongoose = require('mongoose');
var Category = require('./category');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
category: { type: Schema.Types.ObjectId, ref: 'Category', es_schema: Category, es_indexed:true, es_select: 'name'},
name: String,
price: Number,
image: String
});
ProductSchema.plugin(mongoosastic, {
hosts: [
'localhost:9200'
],
populate: [
{path: 'category', select: 'name'}
]
});
module.exports = mongoose.model('Product', ProductSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CategorySchema = new Schema({
name: { type: String, unique: true, lowercase: true} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
和这样的查询:
GET storys/_suggest
{
"story-suggest": {
"text": "bow",
"completion": {
"field": "title"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我很难将其移植到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);
Run Code Online (Sandbox Code Playgroud)
从服务器控制器查询时:
Story.search({
query: {
"match": { title : req.query.q } …
Run Code Online (Sandbox Code Playgroud) 问题:我已经在elasticsearch 1.7.1中创建了映射及其工作正常但在更新到2.1.1之后它会给我异常
例外
response: '{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason"
:"analyzer on field [_all] must be set when search_analyzer is set"}],"type":"ma
pper_parsing_exception","reason":"Failed to parse mapping [movie]: analyzer on f
ield [_all] must be set when search_analyzer is set","caused_by":{"type":"mapper
_parsing_exception","reason":"analyzer on field [_all] must be set when search_a
nalyzer is set"}},"status":400}',
toString: [Function],
toJSON: [Function] }
Run Code Online (Sandbox Code Playgroud)
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"filter": {
"nGram_filter": {
"type": "nGram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": …
Run Code Online (Sandbox Code Playgroud) 我有两个模型,我试图参考.风格和品牌.
Brand填充了所需的对象,但Style始终为空.
我已经尝试清除缓存/删除索引.有和没有include_in_parent和类型:'嵌套'.
我觉得它可能与指定的es_type等有关.不确定.
产品架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Style = require('./style');
var Brand = require('./brand');
var mongoosastic = require('mongoosastic');
var ProductSchema = new mongoose.Schema({
name: { type: String, lowercase: true , required: true},
brand: {type: mongoose.Schema.Types.ObjectId, ref: 'Brand',
es_type:'nested', es_include_in_parent:true},
style: {type: mongoose.Schema.Types.ObjectId, ref: 'Style',
es_schema: Style, es_type:'nested', es_include_in_parent: true},
year: { type: Number }
});
ProductSchema.plugin(mongoosastic, {
hosts: [
'localhost:9200'
],
populate: [
{path: 'style'},
{path: 'brand'}
]
});
Product = module.exports = …
Run Code Online (Sandbox Code Playgroud) 我在MEAN堆栈程序中有mongoosastic设置.一切正常,除非我从mongodb删除文档时,它不会在elasticsearch索引中删除.所以每次我进行包含删除项目的搜索时,都会返回已删除的项目,但是当它被水合时它会为null.mongoosastic处理从ES索引删除?我是否必须编制索引刷新程序?
var mongoose = require('mongoose');
var mongoosastic = require("mongoosastic");
var Schema = mongoose.Schema;
var quantumSchema = new mongoose.Schema({
note: {
type: String,
require: true,
es_indexed: true
}
});
quantumSchema.plugin(mongoosastic);
var Quantum = mongoose.model('Quantum', quantumSchema);
Quantum.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);
}
});
Run Code Online (Sandbox Code Playgroud) 我已经成功配置了mongoosastic,我尝试搜索并且它工作正常,但是当涉及到前端我不确定如何实现这一点时,我尝试了很多方法但是无法想出一个好的解决方案
这是代码.
// For the Search API
router.post('/api/search/', function(req, res, next) {
Job.search(
{
query_string:
{ query: req.body.search }
} , function(err, results) {
if (err) return next(err);
res.json(results);
});
});
Run Code Online (Sandbox Code Playgroud)
因此,每当我搜索与"工程师"相关的内容时,我都会得到一个json数据
所以后端确实完美运行.
但是当涉及到jquery和ajax时,我总是得到不好的请求
逻辑:每当插入某些内容然后发布并找到该结果.
这是前端jquery代码.
$('#search').keyup(function() {
var search_term = $(this).val();
$.ajax({
type: "POST",
url: "/api/search",
success: function(){
$('search_results').html(search_term);
},
error: function(data){
alert('Error', data);
}
});
});
Run Code Online (Sandbox Code Playgroud)
HTML
<input type="text" class="form-control input-lg" id="search" name="search" placeholder="Search for part-time..." />
<div id="search_results">
</div>
Run Code Online (Sandbox Code Playgroud)
如何将json结果插入search_results
?
实际上,我现在想从某些字段中删除索引,任何人都可以告诉我如何为我的代码编写映射;现在谁能告诉我这有什么问题,或者任何人都可以告诉我正确的做法是什么那。
Blog.createMapping(function(err, mapping){
if(err){
console.log('error creating mapping (you can safely ignore this)');
console.log(err);
}else{
mapping: {
properties: {
jeb_no: {
index: "no"
}
}
}
console.log('mapping created!');
console.log(mapping);
}
Run Code Online (Sandbox Code Playgroud)
});
先感谢您。
这是我的查询:
"query":{
"fuzzy":{
"author":{
"value":query,
"fuzziness":2
},
"career_title":{
"value":query,
"fuzziness":2
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Node.js 中回调的一部分。 Query
(作为要比较的值插入)在函数的前面设置。
我需要它能够做的是模糊地检查文档的author
和career_title
,并返回在任一字段中匹配的任何文档。上面的语句永远不会返回任何内容,每当我尝试访问它应该创建的对象时,它都会说它是未定义的。我知道我可以编写两个查询,一个检查每个字段,然后按分数对结果进行排序,但我觉得在每个对象中搜索一个字段两次会比在每个对象中搜索两个字段一次慢。
我想弄清楚如何使用elasticsearch 查询对象中的特定字段。我的索引文档如下所示:
{
name : 'thename'
meta : {
title : {value: 'foo is the title'}
headline: {value : 'bar is the headline'}
}
}
Run Code Online (Sandbox Code Playgroud)
例如,我将如何创建针对 的查询meta.title.value
?
这实际上支持吗?
我可以查询这些值而无需指定键,例如:
{
query: 'foo'
}
Run Code Online (Sandbox Code Playgroud)
我得到了正确的结果,但如果我只想搜索元对象中的特定键,我不知道该怎么做。更具体地说,如果这有什么区别,
我正在使用mongoose
with 。mongoosastic
这是我在elasticsearch上的文档映射:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"properties": {
"value": {
"type": "string"
}
}
},
"headline": {
"properties": {
"value": {
"type": "string"
}
}
},
"visible": …
Run Code Online (Sandbox Code Playgroud) 我使用Mongoosastic将我的模型索引到Elasticsearch,它的工作非常好,只有问题我有它的From和Size for pagination.
根据Mongoosastic api它
完整查询Elasticsearch的DSL通过搜索方法公开
基于Elasticsearch Api From/Size我来到这个代码
music.search( {query_string:{ query:term }},{"from" : 0},{"size" : 10}, { hydrate:true }, function(err,results) {
console.log(results.hits.hits);
})
Run Code Online (Sandbox Code Playgroud)
在runnig之后我想出了这个错误:
/home/app/node_modules/mongoosastic/lib/mongoosastic.js:256
cb(null, res);
^
TypeError: object is not a function
at /home/app/node_modules/mongoosastic/lib/mongoosastic.js:256:9
at respond (/home/app/node_modules/mongoosastic/node_modules/elasticsearch/src/lib/transport.js:256:9)
at checkRespForFailure (/home/app/node_modules/mongoosastic/node_modules/elasticsearch/src/lib/transport.js:203:7)
at HttpConnector.<anonymous> (/home/app/node_modules/mongoosastic/node_modules/elasticsearch/src/lib/connectors/http.js:156:7)
at IncomingMessage.wrapper (/home/app/node_modules/lodash/index.js:3057:19)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16
at process._tickCallback (node.js:448:13)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?提前致谢!
我想在elasticsearch中搜索。我使用 mongoosastic 作为 Elasticsearch 的驱动程序。我想搜索一个字符串是否存在于一个字段中,并且我想搜索另一个字段是否应该完全匹配。我怎样才能在 mongoosastic 中做到这一点?
elasticsearch ×11
mongoosastic ×11
node.js ×8
mongoose ×3
mongodb ×2
ajax ×1
express ×1
jquery ×1