如何通过ElasticSearch和Flashlight查询Firebase? [英] How query Firebase with ElasticSearch and Flashlight?

查看:154
本文介绍了如何通过ElasticSearch和Flashlight查询Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用带有Firebase和Elastic Search的Flashlight进行直接查询时,它确实有效:

查询

  {index:firebase,query:rose,type:tasting} 

$ b 代码

c $ c> ...
Map< String,String> q = new HashMap<>();
q.put(index,firebase);
q.put(type,tasting);
q.put(query,rose);

String key = ref.child(request)。push()。getKey();
ref.child(request)。child(key).setValue(q);
...

但是当我不想仅限于10个提示时, (无结果)

查询

  { 索引: 火力, 选项:{ 从 0 到:50}, 查询:{ QUERY_STRING:{ 查询: 玫瑰}},类型:tasting} 

code

  ... 
HashMap< String,String> q = new HashMap<>();
q.put(query,rose);
qs = new HashMap<>();
qs.put(query_string,q);


映射< String,Object> options = new HashMap<>();
options.put(from,0);
options.put(to,50);

HashMap< String,Object> qo = new HashMap<>();
qo.put(index,firebase);
qo.put(type,tasting);
qo.put(options,options);
qo.put(query,a);

String key = ref.child(request)。push()。getKey();
ref.child(request)。child(key).setValue(q);
...

我使用在Flashlight示例中提供的选项,选项:

$ $ p $ search:{
request:{
$ recid:{
//我只能读取分配给我的记录
.read:auth.id === data.child('id')。val()|| auth.uid === data.child('id')。val(),
//我只能写新的不存在的记录
.write:!data.exists()& &(newData.child('id').val()=== auth.id || newData.child('id').val()=== auth.uid),
。 validate:newData.hasChildren(['query','index','type']),
index:{
//接受数组或字符串
.validate :(newData.isString()& newData.val()。length< 1000)|| newData.hasChildren(),
$ child:{
.validate :newData.isString()& newData.val() h <1000
}
},
type:{
//接受数组或字符串
.validate:(newData.isString( )&& newData.val()。length< 1000)|| newData.hasString()&& amp;& amp; amp; amp;& amp; amp; amp; amp;& newData.val()。length< 1000
}
},
query:{
//查询对象的结构是相当开放的
.validate:newData。 isString()|| newData.hasChildren()

$ other:{
.validate:false
}
}
},
response:{
$ recid:{
//我只能读/写分配给我的记录
.read:auth.id === data .child('id')。val()|| auth.uid === data.child('id')。val(),
.write:auth.id === data.child('id')。 auth.uid === data.child('id').val(),
//假定Flashlight将使用具有admin:true的
/ /登录用户唯一需要做的就是在读取结果后删除结果
.validate:false
}
}
}

为什么选项参数可以在 https://github.com/firebase/flashlight/issues/29#issuecomment-129340229

解决方法

我期望手电筒能够处理所有类型的elasticsearch查询2解决方案:
1 /不是
2 /我没有找到方法!



在我这边,我不得不这样更新Flashlight SearchQueue.js文件:

  var dat = snap.val(); 
var key = snap.key();

//得到你的查询字符串
var q = dat.query.query_string.query;

//建立你的ES查询
var q1 = {query:{match:{_ all:q}}};
$ b $ if(this._assertValidSearch(key,dat)){
//执行(非常简单)ElasticSearch查询
this.esc.search({
index :dat.index,
类型:dat.type,
//添加选项
from:dat.options.from,
size:dat.options.size,
//添加ES查询
body:q1
},函数(error,response){
if(error){
this._reply(key,{error:error,总共:0});
} else {
this._reply(key,response);
}
} .bind(this));
}
},


When I use Flashlight with Firebase and Elastic search with a straight forward query it does work :

query

 {"index":"firebase","query":"rose","type":"tasting"}

code

...
Map<String,String> q = new HashMap<>();
q.put("index", "firebase");
q.put("type", "tasting");
q.put("query", "rose");

String key = ref.child("request").push().getKey();
ref.child("request").child(key).setValue(q);
...

But when I want be not limited to only 10 hints, this failed (no results)

query

{"index":"firebase","options":{"from":0,"to":50},"query":{"query_string":{"query":"rose"}},"type":"tasting"}

code

...
HashMap<String, String> q = new HashMap<>();
q.put("query", "rose");
qs = new HashMap<>();
qs.put("query_string", q);


Map<String, Object> options = new HashMap<>();
options.put("from", 0);
options.put("to", 50);

HashMap<String, Object> qo = new HashMap<>();    
qo.put("index", "firebase");
qo.put("type", "tasting");
qo.put("options", options);
qo.put("query", a);

String key = ref.child("request").push().getKey();
ref.child("request").child(key).setValue(q);
...

I use the option delivered in the Flashlight example, and add part for option :

"search": {
     "request": {
        "$recid": {
           // I can only read records assigned to me
           ".read": "auth.id === data.child('id').val() || auth.uid === data.child('id').val()",
           // I can only write new records that don't exist yet
           ".write": "!data.exists() && (newData.child('id').val() === auth.id || newData.child('id').val() === auth.uid)",
           ".validate": "newData.hasChildren(['query', 'index', 'type'])",
           "index": {
              // accepts arrays or strings
              ".validate": "(newData.isString() && newData.val().length < 1000) || newData.hasChildren()",
              "$child": {
                 ".validate": "newData.isString() && newData.val().length < 1000"
              }
           },
           "type": {
              // accepts arrays or strings
              ".validate": "(newData.isString() && newData.val().length < 1000) || newData.hasChildren()",
              "$child": {
                 ".validate": "newData.isString() && newData.val().length < 1000"
              }
           },
           "query": {
              // structure of the query object is pretty open-ended
              ".validate": "newData.isString() || newData.hasChildren()"
           },
           "$other": {
              ".validate": false
           }
        }
     },
     "response": {
        "$recid": {
           // I can only read/write records assigned to me
           ".read": "auth.id === data.child('id').val() || auth.uid === data.child('id').val()",
           ".write": "auth.id === data.child('id').val() || auth.uid === data.child('id').val()",
           // Assumes that Flashlight will be writing the records using a secret or a token that has admin: true
           // The only thing a logged in user needs to do is delete results after reading them
           ".validate": false
        }
     }
  }

Why the options parameter can be find at https://github.com/firebase/flashlight/issues/29#issuecomment-129340229

解决方案

I was expecting that flashlight would handle all type of elasticsearch queries. 2 solutions : 1/ it doesn't 2/ I didn't find how !

On my side, I had to update the Flashlight SearchQueue.js file like this :

_process: function (snap) {
    var dat = snap.val();
    var key = snap.key();

    // get your query string
    var q = dat.query.query_string.query;

    // build your ES query
    var q1 = {"query":{"match":{"_all":q}}};

    if (this._assertValidSearch(key, dat)) {
        // Perform (a very simple) ElasticSearch query
        this.esc.search({
            index: dat.index,
            type: dat.type,
            // add options 
            from : dat.options.from,
            size : dat.options.size,
            // add ES Query
            body : q1
        }, function (error, response) {
            if (error) {
                this._reply(key, {error: error, total: 0});
            } else {
                this._reply(key, response);
            }
        }.bind(this));
    }
},

这篇关于如何通过ElasticSearch和Flashlight查询Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆