【问题标题】:ElasticSearch Query to filter out certain camerasElasticSearch 查询过滤掉某些摄像头
【发布时间】:2017-01-28 19:51:13
【问题描述】:

我需要创建一个过滤器,它将获取我所有的相机并过滤掉与“蓝图”相关联的相机。这位于我在 MongoDB 中的相机的属性中。我想过滤掉没有"BluePrint" 类型的摄像机。

我认为我需要修复的部分是 getAllCameras 方法中的 query 部分。

self.evaluateCameras = function() {
    self.getAllCameras(function(err, cameras) {
        if(err) {
            console.log(err);
        }
        else {
            // -- publish newly included cameras
            cameras.forEach(function(camera) {
                if(self.includedCameras[camera._id] == undefined) {
                    camera._source.entityId = camera._id;
                    camera._source.systemType = camera._type;
                    self.publish(camera._source, "create")
                    // -- to scale this need to do caching in Redis shared cache across processes
                    self.includedCameras[camera._id] = camera;
                }
            });
        }

        // process any messages received while initializing stream
        self.initComplete = true;
        for(var j = 0; j < self.tempMessageCache.length; j++) {
            var cacheMsg = self.tempMessageCache[j];
            self.evalPublish(cacheMsg);
        }
        self.tempMessageCache = [];

    });
};

self.getAllCameras = function(callback) {
    self.q = {
        "from": 0,
        "size": 10000, // -- todo: implement some kind of paging
        "sort": [
            {'properties.name': 'asc'},
            {'properties.cameraId': 'asc'}
        ],
        "query": {
            "filtered": {
                "query": {
                    "match_all": {}
                },
                "filter": {
                    "and": [
                        {
                            "exists": {"field": "properties.geoRef"}
                        },
                        {
                            "geo_shape": {
                                "properties.geoRef": {
                                    "shape": {
                                        "coordinates": properties.geoRef.coordinates,
                                        "type": "point"
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    };
    elasticClient.search({
        index: 'myproject-now',
        type: 'system.camera',
        body: self.q
    }, function (err, response) {
        if (err)
            callback(err, null);
        else {
            callback(null, response.hits.hits);
        }
    });
};

【问题讨论】:

    标签: javascript jquery mongodb elasticsearch


    【解决方案1】:

    在对 elasticsearch 进行一些研究后,这是我想出的解决方案:

    self.getAllCameras = function(callback) {
        self.q = {
            "from": 0,
            "size": 10000, // -- todo: implement some kind of paging
            "sort": [
                {'properties.name': 'asc'},
                {'properties.cameraId': 'asc'}
            ],
            "query": {
                "filtered": {
                    "query": {
                        "match_all": {}
                    },
                    "filter": {
                        "and": [
                            {
                                "exists": {"field": "properties.geoRef"}
                            },
                            {
                                "not": {
                                    "term": {
                                        "properties.geoRef.type" : "floorplan"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        };
        elasticClient.search({
            index: 'myproject-now',
            type: 'system.camera',
            body: self.q
        }, function (err, response) {
            if (err)
                callback(err, null);
            else {
                callback(null, response.hits.hits);
            }
        });
    };
    

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多