【问题标题】:Mongoosastic: error sorting by distanceMongoosastic:按距离排序错误
【发布时间】:2016-01-02 20:25:54
【问题描述】:

当我尝试使用 Mongoosastic 按距离对搜索结果进行排序时,我收到以下 Elastic Search 错误:

{ 消息:'SearchPhaseExecutionException[未能执行阶段 [query_fetch],所有分片失败;分片失败 {[rQFD7Be9QbWIfTqTkrTL7A][用户][0]:SearchParseException[[用户][0]: 查询[过滤(+关键字:咖啡馆)->GeoDistanceFilter(位置, SLOPPY_ARC, 25000.0, -70.0264952, 41.2708115)],from[-1],size[-1]: 解析失败[解析源失败 [{"timeout":60000,"sort":[{"[object Object]":{}}]}]]];嵌套: SearchParseException[[用户][0]: 查询[过滤(+关键字:咖啡馆)->GeoDistanceFilter(位置, SLOPPY_ARC, 25000.0, -70.0264952, 41.2708115)],from[-1],size[-1]: 解析失败 [未找到 [[object Object]] 的映射以便排序 在]]; }]' }

代码示例见下文:

var query = {
    "filtered": {
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "keywords": "cafe"
                        }
                    }
                ]
            }
        },
        "filter": {
            "geo_distance": {
                "distance": "25km",
                "location": [
                    41.2708115,
                    -70.0264952
                ]
            }
        }
    }
};

var opts = {
    "sort": [
        {
            "_geo_distance": {
                "location": [
                    41.2708115,
                    -70.0264952
                ],
                "order":         "asc",
                "unit":          "km",
                "distance_type": "plane"
            }
        }
    ],
    "script_fields": {
        "distance": "doc[\u0027location\u0027].distanceInMiles(41.2708115, -70.0264952)"
    }
};

User.search(query, opts, function (err, data) {
    if (err || !data || !data.hits || !data.hits.hits || !data.hits.hits.length) {
        return callback(err);
    }

    var total = data.hits.total,
    //page = params.page || 1,
        per_page = query.size,
        from = query.from,
    //to = from +
        page = query.from / query.size,
        rows = data.hits.hits || [];
    for (var i = 0; i < rows.length; i++) {
        rows[i].rowsTotal = total;
    }
    callback(err, toUser(rows, params));
});

这是用户架构:

var schema = new Schema({
    name: {type: String, default: '', index: true, es_type: 'string', es_indexed: true},
    location: {type: [Number], es_type: 'geo_point', es_indexed: true, index: true},
    shareLocation: {type: Boolean, default: false,  es_type: 'boolean', es_indexed: true},
    lastLocationSharedAt: {type: Date},
    email: {type: String, default: '', index: true, es_type: 'string', es_indexed: true},
    birthday: {type: String, default: ''},
    first_name: {type: String, default: ''},
    last_name: {type: String, default: ''},
    gender: {type: String, default: ''},
    website: {type: String, default: '', index: true, es_indexed: true},
    verified: {type: Boolean, default: false},
});

【问题讨论】:

  • 你能更新你的UserSchema的问题吗?

标签: elasticsearch mongoosastic


【解决方案1】:

我也遇到了一个错误,我认为 Mongoosastic 的升级是双重包装代码。它似乎是基于“排序”而不是基于搜索但仍在审查。 Val 似乎对正在发生的事情有更好的了解,因为它可能与用户模式而不是功能有关。

我正在使用类似的架构,只是升级并遇到了问题。

【讨论】: