jbdemonte / mongoose-elasticsearch-xp

A mongoose plugin that indexes models into Elasticsearch 2 / 5 / 6 and 7
93 stars 34 forks source link

Filter Mongoose Reference Data #51

Open farhankhwaja opened 5 years ago

farhankhwaja commented 5 years ago

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??