[英]Elasticsearch Index Data giving wrong results in query
我正在尝试使用Elasticsearch为我的React Web应用程序创建全文搜索,但它不会返回查询的所有结果。 我认为我在批量方法中犯了一些错误,但是我无法理解自己在做错什么。 我的代码是:
// Declare variable to contain body of JSON data for loading to ElasticSearch
let br = [];
// Function to create body for loading to ElasticSearch
function create_bulk (bulk_request) {
let obj;
for (let i = 0; i < res.length; i++) {
obj = res[i];
// Insert header of record
bulk_request.push({index: {_index: 'tweet', _type: 'tweet', _id: i+1}});
bulk_request.push(obj);
}
return bulk_request;
}
// Call function to get body for loading
create_bulk(br);
// Standard function of ElasticSearch to use bulk command
client.bulk(
{
body : br
}, function (err, resp) {
console.log(err);
});
client.search({
index: 'tweet',
type: 'tweet',
body: {
query: {
match: {"text" : "new york" }
},
}
},function (error, response,status) {
if (error){
console.log("search error: "+error)
}
else {
console.log("--- Response ---");
console.log(response);
console.log("--- Hits ---");
response.hits.hits.forEach(function(hit){
console.log(hit);
})
}
});
数据样本,它是JSON数据的数组:
{date: "2014-06-04 11:30:07", text: "New York Today: Honoring our Civil Servants t.co/Lhz9fgt2FW", user_id: "nytimes "}
在批量通话和搜索通话之间,您需要通话刷新以确保所有索引数据都可用于搜索。
client.bulk(
{
refresh: true, <--- add this
body : br
}, function (err, resp) {
console.log(err);
client.search({...}) <--- and make your search call only when the bulk returns
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.