ElasticSearch:使用 OR AND 子句创建 bool 查询

ElasticSearch: Creating a bool query with OR AND clauses

我正在尝试将 sql 查询转换为其等效的 elasticsearch 查询(sql 表格和 ES 索引相似)。它是 AND/OR 的组合。

我有一个 SQL 查询

SELECT DISTINCT N_ID 
FROM   MYTABLE 
WHERE  ( [C_ID] = 4 AND E_ID = 765) 
         OR ( C_ID = 6 AND E_ID = 642 ))

这是我的等效弹性查询

GET  mytable_index/_search
{
    "query": {
        "bool": {
            "should": [{
                    "must": [{
                            "term": {
                                "C_ID": 4
                            }
                        }, {
                            "term": {
                                "E_ID": 765
                            }
                        }
                    ]
                }, {
                    "must": [{
                            "term": {
                                "C_ID": 6
                            }
                        }, {
                            "term": {
                                "E_ID": 642
                            }
                        }
                    ]
                }
            ]
        }
    }
}

但是我得到以下异常:

 {
            "type": "parsing_exception",
            "reason": "[must] query malformed, no start_object after query name",
            "line": 5,
            "col": 14
  }

我在 should

中遗漏了另一个 bool 子句
GET  mytable_index/_search
{
    "query": {
        "bool": {
            "should": [{
                    "bool": {
                        "must": [{
                                "match": {
                                    "C_ID": 4
                                }
                            }, {
                                "match": {
                                    "I_ID": 765
                                }
                            }
                        ]
                    }
                }, {
                    "bool": {
                        "must": [{
                                "match": {
                                    "C_ID": 6
                                }
                            }, {
                                "match": {
                                    "I_ID": 642
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
This problem will not simply  solve using query. For achiving the distinct values you  need aggregations.

Please refer to my query

{
    "query": {
        "bool": {
            "should": [{
                    "bool": {
                        "must": [{
                                "match": {
                                    "C_ID": 4
                                }
                            }, {
                                "match": {
                                    "I_ID": 765
                                }
                            }
                        ]
                    }
                }, {
                    "bool": {
                        "must": [{
                                "match": {
                                    "C_ID": 6
                                }
                            }, {
                                "match": {
                                    "I_ID": 642
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "size":0,
    "aggs": {
    "distinct_values": {
      "terms": {
        "field": "N_ID",
        "size": 1000
      }
    }
  }

}