2

我希望能够在我的 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" : []
                }
            }
        }
    }
}';
4

1 回答 1

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" : []
                }
            }
        }
    }
}';
于 2014-10-27T09:03:56.883 回答