Firebase Flashlight(ElasticSearch)过滤,排序,分页

时间:2016-09-27 01:25:53

标签: node.js search elasticsearch firebase firebase-realtime-database

我正在使用Flashlight Firebase插件

我正在使用此example并且工作正常

在示例中,您可以看到 example.js 文件具有查询方法,如下所示

  // display search results
  function doSearch(index, type, query) {

      var ref = database.ref().child(PATH);
      var key = ref.child('request').push( { index: index, type: type, query: query }  ).key;
      ref.child('response/'+key).on('value', showResults);

  }
当我传递像 JSON

这样的值时,

上面的函数会返回结果

{ index: index, type: type, query: query }

当我尝试传递 JSON

之类的值时,它什么都没有回复
{ index: index, type: type, query: { "from" : 1, "size" : 5 , "query": query }

但是以下 ElasticSearch API 会向我返回结果

http://localhost:9200/firebase/user/_search?q=*mani*&pretty&size=5&from=1

以及如何使用Flashlight过滤查询,如下所示

{
  "query": {
    "filtered": {
        "query": {
            "query_string": {
                "query": "drama"
            }
        },
        "filter": {
            //Filter to apply to the query
        }
    }
  }
}

我使用以下安全规则

{
 "rules": {

  ".read": false,
  ".write": false,
  "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
        }
     }
  }
 }
}

请告诉我如何使用Flashlight执行复杂查询和过滤

1 个答案:

答案 0 :(得分:0)

最后我自己做了

这是解决方案

您需要更新SearchQueue.js

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

    if (this._assertValidSearch(key, dat)) {

      // get your query string

    var q = dat.query.query;
    console.log('search', "test", JSON.stringify(dat, null, 2));
    // build your ES query
    //var q1 = {"query":{"match":{"_all":q}}};
        // Perform (a very simple) ElasticSearch query
        this.esc.search({
            index: dat.index,
            type: dat.type,
            // add options 
            from : dat.query.from,
            size : dat.query.size,
            // add ES Query
            //body : q1
            q:dat.query.query
        }, function (error, response) {
            if (error) {
                this._reply(key, {error: error, total: 0});
            } else {
                this._reply(key, response);
            }
        }.bind(this));
    }
}

并更新Example.js

   // display search results
   function doSearch(index, type, query) {
      var ref = database.ref().child(PATH);
      var jsonOBJ = { 
         index: index, 
         type: type, 
         query: { size:1, from:0, query:query},
      };
      var key = ref.child('request').push(jsonOBJ).key;
      console.log('search', key, JSON.stringify(jsonOBJ, null, 2));
     ref.child('response/'+key).on('value', showResults);
   }