mongoose-elasticsearch-xp icon indicating copy to clipboard operation
mongoose-elasticsearch-xp copied to clipboard

Filter Mongoose Reference Data

Open farhankhwaja opened this issue 6 years ago • 0 comments
trafficstars

I am having issues in filtering a mongo referenced filed. Below is the example:

var AssetSchema = new schema({
	assetId: {type: Number, es_indexed: true, index:true},
	assetMeta: {type: schema.Types.ObjectId, ref: 'Metadata'}
});

var autoPopulateLead = function(next) {
	this.populate("assetMeta");
	next();
};

AssetSchema.
	pre("findOne", autoPopulateLead).
	pre("find", autoPopulateLead);

AssetSchema.plugin(mongoostatic, {
	// client: elasticClient,
	hosts: ["localhost:9200"],
	log: "trace",
	index: "asset_details",
	type: "asset"
});

Now when I try to call search and filter out results depending on the assetMeta.type, I create the below query:

assetInfo.esSearch({
	"bool": {
		"must": [{
			"match_all": {}
		}],
		"filter": [
			{
				"nested": {
					path: "assetMeta",
					query: {
						bool: {
							"filter": [
								{"term": {"assetMeta.type": "ball"}}
							]
						}
					}
				}
			}
		]
	},
},
{
   hydrate: true
}).then(dt=>{
	return res.json(dt);
})
.catch(err=>{
		return res.status(400).send(err);
})

But this query doesn't work as expected and return an error: [nested] failed to find nested object under path [assetMeta]

I was expecting the query to match all docs and then filter all the docs which have assetMeta.type = ball.

Am I doing something wrong or something which is not supported??

farhankhwaja avatar Nov 27 '18 22:11 farhankhwaja