Elasticsearch如果类型不同,则添加附加条件


GET test/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"term": {
"fragmentId": "1"
}
},
{
"term": {
"type": "fragment"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"fragmentId": "1"
}
},
{
"term": {
"type": "cf"
}
},
{
"range" :{
"start": {
"gte": 1,
"lte": 5
}
}
}
]
}
}
]
}
}
}

我正在寻找两个文档,一个文档的片段id=1,类型=片段,而另一个文档,其中片段id=2,类型=";cf〃;并且在1和5之间开始。上面的查询完成了任务,但我需要写两次类型和片段id。有没有一种方法可以只在类型为cf时添加范围条件,基本上是将两个bool组合在一起?

这是您要查找的查询:

{
"query": {
"bool": {
"filter": [
{
"term": {
"fragmentId": "1"
}
}
],
"minimum_should_match": 1,
"should": [
{
"term": {
"type": "fragment"
}
},
{
"bool": {
"filter": [
{
"term": {
"type": "cf"
}
},
{
"range": {
"start": {
"gte": 1,
"lte": 5
}
}
}
]
}
}
]
}
}
}

最新更新