Elasticsearch关键字+范围查询(V-6.2)

时间:2018-05-29 22:34:03

标签: node.js elasticsearch range elasticsearch-6

我正在尝试在Elasticsearch中编写一个查询,以使其与Range过滤器和用户的查询关键字输入一起使用。 我最后写的查询是:

var root = new Node('root'); 

var arr = ["transportation.cars.Mazda",
 "transportation.cars.Honda",
 "transportation.cars.Toyota",
 "transportation.train.lightRail",
 "transportation.train.rapidTransit",
 "transportation.waterVehicle.ferry",
 "transportation.waterVehicle.boats" 
 ]

for(var i of arr){
   var res=i.split(".");
    root.addChild(new Node(res[0]));
    res[0].addChild(new Node(res[1])); 
    res[1].addChild(new Node(res[2])); 
 }

this.addChild = function(node) {
    node.setParentNode(this);
    this.children[this.children.length] = node;
} 
console.log(root);

以上查询无效。

此外,我正在寻找匹配来自elasticsearch的键值对1。

"size": val, //default 10,
"from": 0, //default 0,
"query": {
    "bool": {
        "must": {
            "query_string": {
                "query": search_query //Val coming from user input
            },
            "filter": {
                "range": {
                    "lastmodifieddate": {
                        "gte": '2016-12-09T00:00:00',
                        "lte": '2016-12-20T00:00:00'
                    }
                }
            }
        }
    }
}

有人可以说明如何使其工作,还有一个查询参数,所有记录必须匹配fileType = PDF

TIA

1 个答案:

答案 0 :(得分:0)

您必须将“过滤器”放在“必须”之外。它们都处于不同的背景下。请参阅此文档https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html

{
"query": {
    "bool": {
        "must": {
            "query_string": {
                "query": "user input" 
            }
        },
        "filter": {
            "range": {
                "lastmodifieddate": {
                    "gte": "2014-01-09T00:00:00",
                    "lte": "2014-12-20T00:00:00"
                }
            }
        }
    }
  }
}