我正在使用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执行复杂的查询和过滤

最佳答案

最后我自己做了

这是解决方案

您需要更新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);
   }

关于node.js - Firebase Flashlight(ElasticSearch)过滤,排序,分页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39714693/

10-10 11:36