我试图在单个for循环中运行2个solr查询,以从同一个solr核心获取字段的最小值和最大值。第一个查询运行正常,但是,第二个查询每次都在运行数组的最后一个索引中的值。我的代码是这样的:
router.route('/might_be_interested').post(function (req, res) {
var merchant_id = req.body.merchant_id;
var data_merchants = []
var count1 = 0;
var count2 = 0;
var data = [];
var final_data = [];
var myStrQuery1 = 'q=merchant_id%3A' + merchant_id + '&wt=json&indent=true&group=true&group.field=category';
client_product.search(myStrQuery1, function (err, merchant_categories) {
if (err) {
console.log(err);
return;
}
var no_of_categories = merchant_categories.grouped.category.groups.length;
for (var i = 0; i < no_of_categories; i++) {
var category = merchant_categories.grouped.category.groups[i].groupValue;
console.log(category);
var myStrQuery2 = 'q=category%3A' + category + '&group=true&group.field=merchant_name&wt=json&indent=true';
client_product.search(myStrQuery2, function (err, other_merchants) {
if (err) {
console.log(err);
return;
}
var no_of_merchants = other_merchants.grouped.merchant_name.groups.length;
console.log(no_of_merchants);
for (var j = 0; j < no_of_merchants; j++) {
var merchants = other_merchants.grouped.merchant_name.groups[j].groupValue;
if (data_merchants.indexOf(merchants) === -1) {
data_merchants.push(merchants)
}
}
if (parseInt(count1) == (parseInt(no_of_categories) - 1)) {
console.log(data_merchants)
var distinct_merchant_count = data_merchants.length;
console.log("distinct_merchant_count")
console.log(distinct_merchant_count)
for (var k = 0; k < distinct_merchant_count; k++) {
var distinct_merchant = data_merchants[k];
console.log(distinct_merchant)
var myStrQuery3 = 'q=merchant_name%3A('+distinct_merchant+')&sort=cashback_value+desc&rows=1&wt=json&indent=true';
client_product.search(myStrQuery3, function (err, max_cashback) {
if (err) {
console.log(err);
return;
}
var myStrQuery4 = 'q=merchant_name%3A('+distinct_merchant+')&sort=cashback_value+desc&rows=1&wt=json&indent=true';
client_product.search(myStrQuery4, function (err, min_cashback) {
if (err) {
console.log(err);
return;
}
var maxcashback = max_cashback.response.docs[0].cashback_value;
var mincashback = min_cashback.response.docs[0].cashback_value;
var logolocation = max_cashback.response.docs[0].logo_location;
var merchantid = max_cashback.response.docs[0].merchant_id;
data.push(maxcashback)
data.push(mincashback)
data.push(logolocation)
data.push(merchantid)
final_data.push(data)
data = [];
if (parseInt(count2) == parseInt(distinct_merchant_count) - 1) {
send_response.sendSuccessData(final_data, res);
} else {
console.log("incerase counter");
count2 = parseInt(count2) + 1;
}
});
});
}
console.log("in end");
} else {
console.log("incerase counter");
count1 = parseInt(count1) + 1;
}
});
}
});
});
提前致谢。任何建议将不胜感激。
答案 0 :(得分:0)
看看Solr 6 new Faceting implementation。您可能只能通过商家进行分组,并在一个Solr查询中获得最大值和最小值。
答案 1 :(得分:0)
我使用async.each获得了一个解决方案。它在我的情况下正常工作。这是完整的代码:
router.route('/might_be_interested').post(function (req, res) {
var merchant_name1 = req.body.merchant_name;
var count = 0;
var count3 = 0;
var data = [];
var data2 = [];
var merchants_info = []
var final_json_data = []
var myStrQuery1 = 'q=merchant_name%3A' + merchant_name1 + '&wt=json&indent=true&group=true&group.field=category';
client_product.search(myStrQuery1, function (err, merchant_categories) {
if (err) {
console.log(err);
return;
}
var length = merchant_categories.grouped.category.groups.length;
for (var i = 0; i < length; i++) {
var category = merchant_categories.grouped.category.groups[i].groupValue;
data2.push(category)
}
async.each(data2, function (category, callback) {
var myStrQuery2 = 'q=category%3A' + category + '&wt=json&indent=true&group=true&group.field=merchant_name';
client_product.search(myStrQuery2, function (err, other_merchants) {
if (err) {
console.log("err1");
return;
}
var no_of_merchants = other_merchants.grouped.merchant_name.groups.length;
console.log("other_merchants")
for (var j = 0; j < no_of_merchants; j++) {
var merchants = other_merchants.grouped.merchant_name.groups[j].groupValue;
var index = data.indexOf(merchants);
if (index == -1) {
data.push(merchants);
}
}
callback();
console.log(data)
});
}, function (err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
async.each(data, function (merchant_name, callback) {
console.log("merchant_name")
console.log(merchant_name)
var myStrQuery3 = 'q=merchant_name%3A('+merchant_name+')+AND+cashback_type%3A(percentage)%0A&sort=cashback_value+desc&rows=1&wt=json&indent=true';
client_product.search(myStrQuery3, function (err, max_cashback) {
if (err) {
console.log("err2");
return;
}else{
var myStrQuery4 = 'q=merchant_name%3A('+merchant_name+')+AND+cashback_type%3A(percentage)%0A&sort=cashback_value+desc&rows=1&wt=json&indent=true';
client_product.search(myStrQuery4, function (err, min_cashback) {
if (err) {
console.log("err3");
return;
}
merchants_info.push(merchant_name);
merchants_info.push(max_cashback.response);
merchants_info.push(min_cashback.response);
final_json_data.push(merchants_info)
merchants_info = [];
callback();
});
}
});
}, function (err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('in_loop');
send_response.sendSuccessData(final_json_data, res);
console.log("success");
}
});
console.log('All files have been processed successfully');
}
});
});
});