4.Elasticsearch查询

本文详细介绍了Elasticsearch的查询操作,包括基本查询如match_all、match、multi_match、term和terms,结果过滤,高级查询如bool、range、fuzzy,过滤(filter)以及排序。示例展示了各种查询和过滤条件的使用,帮助理解Elasticsearch的查询机制。
摘要由CSDN通过智能技术生成

目录:
1.安装Elasticsearch
2.Elasticsearch概念
3.Elasticsearch索引
4.Elasticsearch查询
5.Elasticsearch聚合aggregations
6.Spring Data Elasticsearch入门
7.Repository文档操作

现有数据

{
   
  "took": 1,
  "timed_out": false,
  "_shards": {
   
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
   
    "total": 3,
    "max_score": 1,
    "hits": [
      {
   
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
   
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      },
      {
   
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
   
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      },
      {
   
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
   
          "title": "apple手机",
          "images": "3.jpg",
          "price": 6899
        }
      }
    ]
  }
}

1、基本查询

基本语法

GET /索引库名/_search
{
   
    "query":{
   
        "查询类型":{
   
            "查询条件":"查询条件值"
        }
    }
}

这里的query代表一个查询对象,里面可以有不同的查询属性

  • 查询类型:
    • 例如:match_all, matchterm , range 等等
  • 查询条件:查询条件会根据类型的不同,写法也有差异。
1.1、查询所有(match_all)
1.2、匹配查询(match)

match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系。

示例

GET /dxbindex1/_search
{
   
    "query":{
   
        "match":{
   
            "title":"小米电视"
        }
    }
}

返回结果

{
   
  "took": 16,
  "timed_out": false,
  "_shards": {
   
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
   
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
   
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
   
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      },
      {
   
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
   
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}
  • took:查询花费时间,单位是毫秒
  • time_out:是否超时
  • _shards:分片信息
  • hits:搜索结果总览对象
    • total:搜索到的总条数
    • max_score:所有结果中文档得分的最高分
    • hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
      • _index:索引库
      • _type:文档类型
      • _id:文档id
      • _score:文档得分
      • _source:文档的源数据
and关系

某些情况下,我们需要更精确查找,我们希望这个关系变成and

示例

GET /dxbindex1/_search
{
   
    "query":{
   
        "match":{
   
            "title":{
   
                "query": "小米电视",
                "operator": "and"
            }
        }
    }
}

返回结果

{
   
  "took": 7,
  "timed_out": false,
  "_shards": {
   
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
   
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
   
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
   
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      }
    ]
  }
}
co.elastic.clients.elasticsearch.core.aggregations 是 Java 客户端 ElasticSearch 的一个聚合(Aggregation)方法,用于对数据进行分析和统计。 具体使用方法可以参考以下示例: ```java import co.elastic.clients.base.*; import co.elastic.clients.elasticsearch.*; import co.elastic.clients.elasticsearch.core.*; import co.elastic.clients.elasticsearch.core.aggregations.*; import co.elastic.clients.elasticsearch.core.aggregations.bucket.*; import co.elastic.clients.elasticsearch.core.aggregations.metrics.*; import java.io.IOException; import java.util.*; public class ElasticSearchAggregationExample { public static void main(String[] args) throws IOException, ApiException { RestClientBuilder restClientBuilder = RestClient.builder( new HttpHost("localhost", 9200, "http") ); ElasticSearch client = new ElasticSearch(restClientBuilder); SearchRequest request = new SearchRequest() .index("my_index") .source(new SearchSource() .query(new MatchAllQuery()) .aggregations(new TermsAggregation("my_terms_agg") .field("my_field") .size(10) .subAggregations(new AvgAggregation("my_avg_agg") .field("my_other_field") ) ) ); SearchResponse response = client.search(request); TermsAggregationResult myTermsAggResult = response.aggregations().terms("my_terms_agg"); for (TermsAggregationEntry entry : myTermsAggResult.buckets()) { String term = entry.keyAsString(); long count = entry.docCount(); AvgAggregationResult myAvgAggResult = entry.aggregations().avg("my_avg_agg"); double avg = myAvgAggResult.value(); System.out.println(term + ": " + count + ", avg: " + avg); } client.close(); } } ``` 这个例子展示了如何使用 co.elastic.clients.elasticsearch.core.aggregations 方法来进行聚合查询。在这个例子中,我们使用了 TermsAggregation 和 AvgAggregation 两个聚合方法,对数据进行了分组和统计。具体步骤为: 1. 创建一个 SearchRequest 对象,并设置索引名称和查询条件。 2. 在查询条件中添加聚合条件。这里使用了 TermsAggregation 来对数据进行分组,然后使用 AvgAggregation 来统计每个分组的平均值。 3. 执行查询,并获取查询结果。 4. 使用聚合结果对象的方法来获取聚合结果,然后对结果进行处理。 需要注意的是,聚合方法的具体参数和用法可以参考 ElasticSearch 官方文档。同时,Java 客户端的版本和 ElasticSearch 的版本也需要匹配,否则可能会出现兼容性问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值