ES查询语句实例

本文深入探讨了Elasticsearch中复杂查询语句的构造方法,包括布尔查询、范围查询、存在查询等,并展示了如何结合filter和post_filter优化查询效率。通过实际案例,读者将学会如何灵活运用各种查询类型来精准定位所需数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Should

GET /test/_search
{
  "query": {
    "bool": {
        "should": [
          {
	       	"bool": {
	        	"must": [
	        		{
                  "term": {
                    "cf3.c42.c4.keyword": "1000000000"
                  }
                },
                {
                  "term": {
                    "cf3.d1.c4.keyword": {
                      "value": "1000000001"
                    }
                  }
                }
	        	]
	       	}
       },
       {
       		"bool": {
        		"must": [
           			{
                  "term": {
                    "cf3.c42.c4.keyword": "1000000001"
                  }
                },
                {
                  "term": {
                    "cf3.d1.c4.keyword": {
                      "value": "1000000000"
                    }
                  }
                }
         		]
       		}
       }
        ]
    }
  }
}

filter

GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "cf1.keyword": {
              "value": "0000000001"
            }
          }
        }
      ],
      "filter": [
            {
            "range": {
              "cf3.c26.keyword": {
                "gte": 34.78,
                "lte": 50
              }
            }
          },
          {
            "range": {
              "cf3.c27.keyword": {
                "gte": 116.65,
                "lte": 150
              }
            }
          }
        ]
      }
    }
    

A and (B or C)写法如下:

GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "cf8": {
              "value": "reg8"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "cf1.keyword": {
                    "value": "0000000000"
                  }
                }
              },
              {
                "term": {
                  "cf1.keyword": {
                    "value": "0000000001"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 100
}

(A and B) or (C and D)写法如下:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match_phrase": {
                  "A": "你好"
                }
              },
              {
                "match_phrase": {
                  "B": "头"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "match_phrase": {
                  "C": "美国"
                }
              },
              {
                "match_phrase": {
                  "D": "世界"
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 100
}

_all与term同时使用:

查询同时,通过filter条件在不影响打分的情况下筛选出想要的数据

简单的filter查询

实例1:先查询index为library,type为books的全部文档;再过滤price等于20的

分词查询,然后精确搜索

GET /test/_search
{
    "query":{
        "match":{
            "_all":"XXX"
        }
    },
    "post_filter":{
        "term":{
            "person.age":"20"
        }
    }
}

另一种写法:

GET /test/_search
{
    "query":{
        "bool":{
            "must":[
                "match":{
                    "_all":"XXX"
                }
            ],
            "filter":{
                 "bool":{
                    "must":[
                        {
                             "term":{
                                "person.name":"jack"
                            }
                        },
                        {
                            "term":{
                                "person.age":"27"
                            }
                        }
                    ]
                }
            }
        }
    }
}

分词查询,然后模糊搜索,使用match_phrase可以对输入的搜索语句进行分词

GET /test/_search
{
    "query":{
        "match":{
            "_all":"XXX"
        }
    },
    "post_filter":{
        "wildcard":{
            "person.name":"*<>*"
        }
    }
}

判断查询某个字段存在

GET test/_search
{
    "query": {
    	"exists":{
    		"field": "person.name"
    	}
    }
}

range(a,b) AND (C Or D)

{
	"size": 0,
	"query": {
		"bool": {
			"must": [
				"bool": {
					"should": [
						"term": {
							"A": {
								"value": "001"
							}
						},
						"term": {
							"B": {
								"value": "002"
							}
						}
					]
				},
				"range": {
					"start_time": {
						"from": "C",
						"to": "D"
					}
				}
			]
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值