弹性搜索 - 在bool查询

时间:2016-10-26 08:09:50

标签: elasticsearch

我正在形成一个弹性搜索查询,如下所示。我想动态遍历values数组并构造查询而不是硬编码数组成员。

 var myQuery = q.Bool(bb => bb.Must(fi => fi.Term("_type", "myValue"),
                    fi => fi.Term("tags", values[0]),
                    fi => fi.Term("tags", values[1]),
                    fi => fi.Term("tags", values[2]),
                    fi => fi.Term("tags", values[3]),
                    fi => fi.QueryString(ques => ques.Query(query))));

我想做类似下面的事情,因为values数组中的项目数量可能会不时出现

  var myQuery = q.Bool(bb => bb.Must(fi => fi.Term("_type", "myValue"),
                   foreach(item in Values)
                   {
                     fi => fi.Term("tags", item),
                   }
                    fi => fi.QueryString(ques => ques.Query(query))));

2 个答案:

答案 0 :(得分:0)

你可以这样做,

var Singleton = (function () {
    var instance;
    var parameter;

    function createInstance() {
        var object = new Object("I am the instance");
        return object;
    }

    return {
        getInstance: function (doc_id) {    
            if (!instance || doc_id !== parameter) {
                instance = createInstance();
                parameter = doc_id;
            }
            return instance;
        }
    };
})();


var instance1 = Singleton.getInstance(doc_id);

答案 1 :(得分:0)

搞定了。

  var myQuery = q.Bool(bb =>bb.Must(fi =>
  {
    var ff = fi.Term("_type", myValue);  
    ff = ssTerms.Aggregate(ff, (current, term) => current & fi.Term("tags", term));
    ff &= fi.QueryString(ques => ques.Query(query));
    return ff;
  });