简体   繁体   English

如何使用ElasticSearch和Flashlight查询Firebase?

[英]How query Firebase with ElasticSearch and Flashlight?

When I use Flashlight with Firebase and Elastic search with a straight forward query it does work : 当我将Flashlight与Firebase和Elastic Search与直接查询一起使用时,它确实起作用:

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) 但是,当我希望不仅限于10条提示时,此操作将失败(无结果)

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 : 我使用Flashlight示例中提供的选项,并为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 为什么可以在https://github.com/firebase/flashlight/issues/29#issuecomment-129340229中找到options参数

I was expecting that flashlight would handle all type of elasticsearch queries. 我期望手电筒可以处理所有类型的elasticsearch查询。 2 solutions : 1/ it doesn't 2/ I didn't find how ! 2个解决方案:1 /不会2 /我没找到方法!

On my side, I had to update the Flashlight SearchQueue.js file like this : 在我这边,我必须像这样更新Flashlight SearchQueue.js文件:

_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));
    }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM