[英]Join query with ElasticSearch
I need to do a join query on firebase using elasticsearch, can anyone help me? 我需要使用elasticsearch在firebase上进行连接查询,任何人都可以帮助我吗?
In particular I have two nodes, in the child node I have a field that is the id of the father node and I would like to have as a result all the fields of the father node. 特别是我有两个节点,在子节点中我有一个字段,它是父节点的id,我希望得到父节点的所有字段。
How do I build my index in the code? 如何在代码中构建索引?
In adding, in my client I use elasticsearchclient, here is an extract of the code to index a node: 另外,在我的客户端中我使用elasticsearchclient,这里是索引节点的代码的摘录:
var db = admin.database();
var etest = db.ref(type);
etest.on('child_added', createOrUpdateIndex);
etest.on('child_changed', createOrUpdateIndex);
etest.on('child_removed', removeIndex);
function createOrUpdateIndex(snap) {
client.index(index, type, snap.val(), snap.key)
.on('data', function(data) { console.log('indexed', snap.key + data ); })
.on('error', function(err) { console.log('index error ', err); }).exec();
}
function removeIndex(snap) {
client.deleteDocument(index, type, snap.key, function(error, data) {
if( error ) console.error('failed to delete', snap.key, error);
else console.log('deleted', snap.key);
});
}
And to take query results: 并获取查询结果:
var queue = db.ref("search");
queue.child('request').on('child_added', processRequest);
function processRequest(snap) {
console.log('process...... ');
snap.ref.remove(); // clear the request after we receive it
var data = snap.val();
// Query ElasticSearch
client.search(index, data.type, { "query": { 'query_string': {
"query" : data.query
}}})
.on('data', function(results) {
var res = JSON.parse(results);
console.log(JSON.stringify(res.hits));
console.log("TOTAL " + JSON.stringify(res.hits.total));
queue.child('response/'+snap.key).set(results);
})
.on('error', function(error){ console.log("errore"); }).exec();
}
Thank you in advance 先感谢您
There are two ways to store your data. 有两种方法可以存储您的数据。 First is creating a nested doc.
首先是创建一个嵌套的doc。 This is helpful if you need to perform search on some of the values and need other info only for the display.
如果您需要对某些值执行搜索并仅需要显示的其他信息,这将非常有用。
PUT /my_index {
"mappings": {
"yourIndex": {
"properties": {
"yourColumn": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"parentNode": { "type": "string" },
"childNode": { "type": "string" }
}
}
}
}}}
Eg. 例如。
'str1', 'parent1', 'child1' 'str2' 'parent1', 'child2' 'str1','parent1','child1''str2''parent1','child2'
If you need not maintain any hierarchy, you can simply create 3 separate columns and store like a flat data. 如果您不需要维护任何层次结构,则只需创建3个单独的列并像平面数据一样存储。
You can specify parent1 id and search for all the docs in both the cases. 您可以指定parent1 id并在两种情况下搜索所有文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.