【问题标题】:Algolia Geo Search Not Working阿尔及利亚地理搜索不起作用
【发布时间】:2016-08-09 02:46:48
【问题描述】:

我在使用 Algolia 的地理搜索功能时遇到了一些问题,该功能以前可以正常工作。这是record of interest

我也将其索引为described by the doc,以便我按最近的距离对其进行排序:

'attributesToIndex' => ['name', 'description', 'geo']

在我的客户端脚本中:

let settings = {
   aroundLatLng: '10.309813,123.893154', 
   getRankingInfo: true, 
   aroundRadius: 2000 
};

index.search(keyword, settings, (err, data) => {
   console.log(data);
});

但这给了我0的打击。注意aroundLatLng 的值——它与record of interest 的值相同。

我错过了什么吗?

【问题讨论】:

  • 我什至在 Algolia 搜索浏览器中尝试过,但仍然没有成功。

标签: php search full-text-indexing algolia


【解决方案1】:

我已经按照文档中给出的 node.js 实现了相同的要求,并且工作正常。 在这里,我正在复制我的整个代码。希望对您有所帮助。

代码

   /*
      I have used async.water model to create the setting of the index and then searching data as per given parameter. few function is custom so no need to bother about that. read each line patently.
   */
    try {
        var self = this;
        var post = req.body;
        var user_id = post.user_id;
        var created_mode = post.user_mode == 'requester' ? 'provider' : 'requester';
        var kword = post.kword;
        var geo = post.geo_loc;
        var aroundLatLng = post.aroundLatLng;
        var aroundRadius = !cmnFn.empty(post.radious) ? post.radious : 4500;
        var hitsPerPage = !cmnFn.empty(post.hitsPerPage) ? post.hitsPerPage : 20;
        var offset = !cmnFn.empty(post.offset) ? post.offset : 0;
        var length = !cmnFn.empty(post.length) ? post.length : 50;
        var budget_from = !cmnFn.empty(post.budget_from) ? post.budget_from : 0;
        var budget_to = !cmnFn.empty(post.budget_to) ? post.budget_to : 0;
        var day_preference = !cmnFn.empty(post.day_preference) ? post.day_preference : '';
        var time_preference = !cmnFn.empty(post.time_preference) ? post.time_preference : '';
        var start_date = !cmnFn.empty(post.start_date) ? post.start_date : '';

job_index 是在 Algolia 上创建的索引

        var job_index = algClient.initIndex('jobs');


        var cond = {};

如果您使用的是构面和过滤器,那么您需要使用过滤器键来执行您的条件,就像您在 sql 中使用 where clouse 所做的一样

        cond.filters = 'created_mode:"' + created_mode + '" AND (NOT user_id:"' + user_id + '")';
        // Query which need to be search
        if (!cmnFn.empty(kword)) {
            cond.query = !cmnFn.empty(post.kword) ? post.kword : '';
        }
        if ((!cmnFn.empty(budget_from) && !cmnFn.empty(budget_to)) && budget_from > 0) {
            cond.filters += ' AND min_charge: ' + budget_from + ' TO ' + budget_to;
        }
        if (!cmnFn.empty(day_preference)) {
            cond.filters += ' AND day_preference:"' + day_preference + '"';
        }
        if (!cmnFn.empty(time_preference)) {
            cond.filters += ' AND time_preference:"' + time_preference + '"';
        }
        if (!cmnFn.empty(start_date) && (new Date(start_date)).getTime() > 0) {
            cond.filters += ' AND start_date:"' + start_date + '"';
        }

我在这里设置 aroundLatLng 以获取距离最近的数据

 /*
  Do not fogot one thing, before using geo search, your records must have _geoloc key having following format
    "_geoloc": {
     "lat": 40.639751,
     "lng": -73.778925
    }

 */
        // Around geo search by given lat lng
        if (!cmnFn.empty(aroundLatLng) && !cmnFn.empty(aroundLatLng.lat)) {
            cond.aroundLatLng = aroundLatLng.lat + ', ' + aroundLatLng.lng;
            if (!cmnFn.empty(aroundRadius) && cond.aroundRadius > 0) {
                cond.aroundRadius = aroundRadius;
            }
        }



        // total number of searched record
        if (!cmnFn.empty(hitsPerPage)) {
            cond.hitsPerPage = hitsPerPage;
        }

        // Record starting position
        if (!cmnFn.empty(offset)) {
            cond.offset = offset;
        }

        // Page Limitation
        if (!cmnFn.empty(length)) {
            cond.length = length;
        }

        // Don't show attributesToHighlight in result set
        cond.attributesToHighlight = false;
        /* 
            restrictSearchableAttributes: List of object key, where to search in given list defined in searchableAttributes
        */
        cond.restrictSearchableAttributes = [
            'user_id',
            'title',
            'description',
            '_geoloc'
        ];
        /* 
            It will return raning info of result when search come under geo search
            Following output will return
            "_rankingInfo": {
                "nbTypos": 0,
                "firstMatchedWord": 0,
                "proximityDistance": 0,
                "userScore": 31,
                "geoDistance": 9, // Calculated distance between data geolocation given in _geoloc and search criteria in aroundLatLng
                "geoPrecision": 1,
                "nbExactWords": 0,
                "words": 1,
                "filters": 0,
                "matchedGeoLocation": {
                    "lat": 28.5503,
                    "lng": 77.2501,
                    "distance": 9
                }
            }
        */
        cond.getRankingInfo = true;
        async.waterfall([
            function (callback) {
                job_index.setSettings({
                    'attributesForFaceting': ['user_id', 'created_mode', 'min_charge', 'day_preference', 'time_preference', 'start_date'],
                    /* 
                        searchableAttributes: List of object key , where to search
                        eg: ['title', 'description']
                        Like in sql: Where title='your searched text' AND description='your searched text'
                        _geoloc is reserved keyword of algolia which will used to search geo location
                     */
                    searchableAttributes: [
                        'title',
                        'description',
                        'user_id',
                        '_geoloc'
                    ],
                    /* 
                        attributesToRetrieve: Here you can specify list of key name which you want to retrive
                        eg: ['name','address','age']
                        Like in sql: Select name, address, age
                    */
                    attributesToRetrieve: [
                        '*'
                    ]
                }).then(() => {
                    return callback(null, 'success');
                });
            }
        ], function (err, results) {
            if (err) {
                console.log('error: ' + err);
            }
            job_index.search(cond).then(results => {
                if (results.nbHits > 0) {
                    var rows = new Array();
                    for (i in results.hits) {
                        var row = {};
                        var item = results.hits[i];
                        var user_info = {};
                        user_info = item.user_info;

                        // Get distance and calculate
                        if (!cmnFn.empty(item._rankingInfo)) {
                            item.distance = cmnFn.meterToKM(item._rankingInfo['geoDistance']);
                        } else {
                            let loc = {
                                geoLoc_1: { latitude: aroundLatLng.lat, longitude: aroundLatLng.lng },
                                geoLoc_2: { latitude: item._geoloc.lat, longitude: item._geoloc.lng }
                            }
                            cmnFn.getDistance(loc, function (distance) {
                                item.distance = distance
                            })
                        }
                        /* self.isFav({ user_id: item.user_id, job_id: item.job_id }), function (err, flag) {
                            item.is_favorite = flag;
                        }; */
                        self.isFav({ user_id: item.user_id, job_id: item.job_id }).then(function (flag) {
                            item.is_favorite = flag;
                        }, function (err) {
                            item.is_favorite = false;
                        });
                        if (cmnFn.empty(item.currency)) {
                            item.currency = "₹";
                        }
                        //Delete few key from object which does not need to send in response
                        delete item['user_info'];
                        delete item['objectID'];
                        delete item['_geoloc'];
                        delete item['_rankingInfo'];
                        row.job_info = item;
                        row.user_info = user_info;
                        rows.push(row);
                    }
                    info = { status: 1, message: util.format(lang.TOTAL_RECORD_FOUND, results.nbHits), data: rows };
                    cmnFn.showMsg(res, info);
                } else {
                    info = { status: 0, message: lang.RECORD_NOT_FOUND, data: null };
                    cmnFn.showMsg(res, info);
                }
            }).catch(err => {
                console.log(err);
                info = { status: 0, message: lang.RECORD_NOT_FOUND, data: null };
                cmnFn.showMsg(res, info);
            });
            //res.end('' + JSON.stringify(results));
        });
    } catch (error) {
        info = { status: 0, message: lang.RECORD_NOT_FOUND, data: null };
        cmnFn.showMsg(res, info);
    }

【讨论】:

    【解决方案2】:

    我的错。 _geoloc 的索引数据格式错误。应使用latlng 键入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多