ES查询语法介绍及举例

1. ES查询语法

Elasticsearch(ES)的查询语法非常强大且灵活,允许执行从简单到复杂的各种搜索操作。ES查询主要基于两种类型:Leaf Query Clauses(叶查询子句)和Compound Query Clauses(复合查询子句)。以下是ES查询语法的全面介绍:

1. Leaf Query Clauses

叶查询子句直接对数据进行查询。常用的叶查询子句包括:

  • Match Query:对文本进行全文搜索,支持文本分析。

    { "match": { "field": "text" } }
    
  • Term Query:对文本进行精确搜索,不分析文本。

    { "term": { "field": "value" } }
    
  • Range Query:基于范围的查询,用于数字、日期等类型。

    { "range": { "field": { "gte": 10, "lte": 20 } } }
    
  • Exists Query:检查文档中是否存在某个字段。

    { "exists": { "field": "name" } }
    
  • Wildcard Query:使用通配符的模糊查询。

    { "wildcard": { "field": "val*" } }
    
  • Regexp Query:使用正则表达式的查询。

    { "regexp": { "field": "regex" } }
    

2. Compound Query Clauses

复合查询子句允许将多个查询组合成一个逻辑查询。常用的复合查询子句包括:

  • Bool Query:将多个查询组合使用布尔逻辑。

    {
      "bool": {
        "must": [{...}],
        "should": [{...}],
        "must_not": [{...}],
        "filter": [{...}]
      }
    }
    
  • Dis Max Query:对子查询的最佳匹配进行打分和排序。

    { "dis_max": { "queries": [{...}] } }
    
  • Constant Score Query:将查询的分数设置为常数。

    { "constant_score": { "filter": {...} } }
    
  • Function Score Query:根据特定函数修改查询的分数。

    {
      "function_score": {
        "query": {...},
        "functions": [{...}]
      }
    }
    

3. Full Text Queries

全文查询专用于处理文本数据,提供全面的文本搜索能力。

  • Match Query:对全文字段进行搜索。
  • Multi Match Query:在多个字段上执行相同的Match查询。
  • Match Phrase Query:搜索完全匹配指定短语的文档。

4. Joining Queries

连接查询用于处理嵌套和父子关系的数据。

  • Nested Query:在嵌套对象上执行查询。
  • Has Child/Has Parent Query:查询子文档或父文档。

5. Geo Queries

地理查询用于处理包含地理位置数据的文档。

  • Geo Distance Query:查询距离某个点一定范围内的文档。
  • Geo Bounding Box Query:查询位于指定矩形区域内的文档。

6. Specialized Queries

专门的查询用于特定场景。

  • More Like This Query:查找与给定文档类似的文档。
  • Script Query:使用脚本进行自定义查询。

7. Aggregation

虽然不是查询语句,但聚合是ES中的一个重要特性,用于对数据进行分组和统计。

Elasticsearch的查询语法覆盖了从简单的全文搜索到复杂的数据分析的各种需求。了解和掌握这些查询语法可以帮助开发者有效地利用Elasticsearch处理和分析

2. ES查询业务举例

创建复杂的Elasticsearch查询语句需要深入了解你的数据结构和业务需求。下面我将提供一些示例查询,并解释它们的功能。这些查询覆盖了不同的场景和Elasticsearch的高级特性。

1. 多条件匹配查询

查询user_data索引中名字为John并且年龄在3040岁之间的用户:

GET /user_data/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "John" }},
        { "range": { "age": { "gte": 30, "lte": 40 }}}
      ]
    }
  }
}

2. 全文搜索与高亮显示

blog_posts索引中搜索包含Elasticsearch tutorial的博客文章,并高亮显示匹配的文本:

GET /blog_posts/_search
{
  "query": {
    "match": { "content": "Elasticsearch tutorial" }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}

3. 聚合查询

统计orders索引中每个用户的订单数量:

GET /orders/_search
{
  "size": 0,
  "aggs": {
    "user_orders": {
      "terms": { "field": "user_id" },
      "aggs": {
        "order_count": { "value_count": { "field": "order_id" }}
      }
    }
  }
}

4. 模糊搜索

products索引中对产品名称进行模糊搜索,查找类似iphon的产品:

GET /products/_search
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "iphon",
        "fuzziness": "AUTO"
      }
    }
  }
}

5. 地理位置搜索

restaurants索引中搜索距离某个坐标5km范围内的餐厅:

GET /restaurants/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "5km",
          "location": {
            "lat": 40.715,
            "lon": -73.988
          }
        }
      }
    }
  }
}

6. 嵌套查询

查询employee_data索引中,有技能名称为Java且水平为expert的员工:

GET /employee_data/_search
{
  "query": {
    "nested": {
      "path": "skills",
      "query": {
        "bool": {
          "must": [
            { "match": { "skills.name": "Java" }},
            { "match": { "skills.level": "expert" }}
          ]
        }
      }
    }
  }
}

7. 多索引搜索

同时在blogsnews索引中搜索包含Elasticsearch的文章:

GET /blogs,news/_search
{
  "query": {
    "match": { "content": "Elasticsearch" }
  }
}

8. 时间范围查询

查询events索引中在2020-01-012020-12-31之间发生的事件:

GET /events/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "2020-01-01",
        "lte": "2020-12-31"
      }
    }
  }
}

9. 正则表达式查询

users索引中搜索电子邮件地址符合特定正则表达式的用户:

GET /users/_search
{
  "query": {
    "regexp": {
      "email": ".*@example\\.com"
    }
  }
}

10. 脚本查询

使用脚本在products索引中计算并查询符合某个条件的产品(例如,价格与税的总和大于100):



GET /products/_search
{
  "query": {
    "script": {
      "script": {
        "source": "doc['price'].value + doc['tax'].value > 100"
      }
    }
  }
}

这些查询示例展示了Elasticsearch在处理各种复杂搜索和数据分析任务方面的强大能力。根据具体的业务需求,可以进一步定制和优化这些查询以获得最佳的搜索体验和性能。

  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值