2

I would like to be able to exclude a number of nodes in my ElasticSearch query. Currently this code is working perfectly, but it will only exclude two nodes ($nodeId1 and $nodeId2).

How would I change this query so I can use an array which would contain as many node IDs as I want? Thank you.

$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 Answer 1

2

You can use the terms filter:

$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" : []
                }
            }
        }
    }
}';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Dan. That is exactly the information I needed and your example worked great. I ended up creating a simple $whereclause and now I can easily add as many nodeIds as I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.