ElasticSearch使用数组搜索bool

时间:2014-10-27 08:11:39

标签: arrays filter elasticsearch boolean-search

我希望能够在我的ElasticSearch查询中排除许多节点。目前这段代码运行正常,但它只会排除两个节点($ nodeId1和$ nodeId2)。

如何更改此查询以便我可以使用包含任意数量的节点ID的数组?谢谢。

$data_string = '{
    "from" : 0, "size" : "' . $maximumResults . '",
    "query": {
        "filtered": {
            "query": {
                "match" : {
                    "thread.title" : {
                        "query" : "' . $threadTitle . '",
                        "operator": "or"
                    }
                }
            },
            "filter": {
                "bool" : {
                    "must" : [],
                    "must_not" : [
                        {
                            "term" : {"thread.discussion_id" : "' . $currentThreadId . '"}
                        }, 
                        { 
                            "term" : {"thread.node" : "' . $nodeId1 . '"}
                        },
                        { 
                            "term" : {"thread.node" : "' . $nodeId2 . '"}
                        }                                       
                    ],
                    "should" : []
                }
            }
        }
    }
}';

1 个答案:

答案 0 :(得分:2)

您可以使用术语过滤器:

$data_string = '{
    "from" : 0, "size" : "' . $maximumResults . '",
    "query": {
        "filtered": {
            "query": {
                "match" : {
                    "thread.title" : {
                        "query" : "' . $threadTitle . '",
                        "operator": "or"
                    }
                }
            },
            "filter": {
                "bool" : {
                    "must" : [],
                    "must_not" : [
                        {
                            "term" : {"thread.discussion_id" : "' . $currentThreadId . '"}
                        }, 
                        { 
                            "terms" : {"thread.node" : ["' . $nodeId1 . '", "' . $nodeId2 . '", "' . $nodeId3 . '"}
                        }                                       
                    ],
                    "should" : []
                }
            }
        }
    }
}';