elasticsearch常用应用操作

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: elasticsearch常用应用操作,会批量从mysql批量导入数据。会解决一些mysql不太好写的sql语句,如搜索多模糊查询。

elasticsearch常用应用操作,会批量从mysql批量导入数据。

会解决一些mysql不太好写的sql语句,如搜索多模糊查询。

然后,重要特性可以提高查询效率,为搜索而生。

部分编辑

GET _search
{
  "query": {
    "match_all": {}
  }
}
get goods/_search
get goods/_search
{
  "query": {
    "term": {
      "categoryName": {
        "value": "手机"
      }
    }
  }
}
get goods/_search
{
  "query": {
    "match": {
      "title": "手机"
    }
  },
  "size": 500
}
get goods/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "手*"
      }
    }
  }
}
get goods/_search
{
  "query": {
    "regexp": {
      "title": "(.)*"
    }
  }
}
get goods/_search
{
  "query": {
    "prefix": {
      "brandName": {
        "value": "2dff"
      }
    }
  }
}
get goods/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1000,
        "lte": 1000
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
    ]
}
get goods/_search
get goods/_search
{
  "query": {
    "query_string": {
      "fields": ["title","categoryName","brandName"],
      "query": "手机",
      "default_operator": "AND"
    }
  }
}
get goods/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "brandName": {
              "value": "小米"
            }
          }
        }
      ]
    }
  }
}
get goods/_search
{
  "query": {
    "match": {
      "title": "手机"
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": "<font color='red'>",
        "post_tags": "</font>"
      }
    }
  }
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.swing.text.Highlighter;
import java.io.IOException;
import java.util.*;
/**
 * @author: samxie
 * @create: 2022/6/10
 * @Description:
 * @FileName: ElasticsearchTest
 * @History:
 * @自定义内容:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchTest {
    @Resource
    private RestHighLevelClient client;
    private final String INDEX = "goods";
    //导入
    @Data
    class Goods {
        private Long id ;
        private String title;
        private Double price;
        private String stock;
        private Integer saleNum;
        private Date createTime;
        private String categoryName;
        private String brandName;
        private Map spec;
        @JSONField(serialize = false)//在转换JSON时,忽略该字段
        private String specStr;
        public Goods(Long id, String title, Double price, String stock, Integer saleNum,
                     Date createTime, String categoryName, String brandName, Map spec,
                     String specStr) {
            this.id = id;
            this.title = title;
            this.price = price;
            this.stock = stock;
            this.saleNum = saleNum;
            this.createTime = createTime;
            this.categoryName = categoryName;
            this.brandName = brandName;
            this.spec = spec;
            this.specStr = specStr;
        }
    }
    @Test
    public void test9() throws IOException {
        List<Goods> goodsList = new Vector<>();
        goodsList.add(new Goods(5L, "这家店", 10.00, null,
                123, new Date(), "手机帝国1", "华为pro",
                null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}"));
        goodsList.add(new Goods(6L, "这家店2", 1102.00, null,
                123, new Date(), "手机22哈", "华为时间皮卡手机pro",
                null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}"));
        goodsList.add(new Goods(7L, "这家店33", 1202.00, null,
                123, new Date(), "2233", "华为pro",
                null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}"));
        goodsList.add(new Goods(8L, "这家店", 1023.00, null,
                123, new Date(), "222", "2dffd",
                null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}"));
        //bulk import
        BulkRequest bulkRequest = new BulkRequest();
        for (Goods goods : goodsList) {
            String specStr = goods.getSpecStr();
            Map map = JSON.parseObject(specStr, Map.class);
            goods.setSpec(map);
            String toJSONString = JSON.toJSONString(goods);
            IndexRequest indexRequest = new IndexRequest(INDEX).source(toJSONString, XContentType.JSON);
            System.out.println("=======" + JSON.toJSONString(indexRequest));
            bulkRequest.add(indexRequest);
        }
        System.out.println("----" + bulkRequest);
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }
    @Test
    public void test34() throws IOException {
        //根据条件查询
        SearchRequest searchRequest = new SearchRequest("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        RangeQueryBuilder query = QueryBuilders.rangeQuery("price");
        query.gte(200);
        query.lte(1002);
        sourceBuilder.query(query);
        sourceBuilder.sort("price", SortOrder.DESC);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        long value = hits.getTotalHits().value;
        System.out.println("总数:" + value);
        SearchHit[] hits1 = hits.getHits();
        for (SearchHit searchHit : hits1) {
            String sourceAsString = searchHit.getSourceAsString();
            System.out.println(sourceAsString);
        }
    }
    @Test
    public void test35() throws IOException {
        //布尔查询根据条件查询
        SearchRequest searchRequest = new SearchRequest("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
        boolQuery.must(termQueryBuilder);
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("title", "手机");
        boolQuery.filter(matchQuery);
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("price");
        rangeQuery.gte(200);
        rangeQuery.lte(1002);
        boolQuery.filter(rangeQuery);
        sourceBuilder.query(rangeQuery);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        long value = hits.getTotalHits().value;
        System.out.println("总数:" + value);
        SearchHit[] hits1 = hits.getHits();
        for (SearchHit searchHit : hits1) {
            String sourceAsString = searchHit.getSourceAsString();
            System.out.println(sourceAsString);
        }
    }
    @Test
    public void test99() throws IOException {
        SearchRequest searchRequest = new SearchRequest("goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "手机");
        sourceBuilder.query(queryBuilder);
        TermsAggregationBuilder aggregation = AggregationBuilders.terms("goods_brands").field("brandName").size(100);
        sourceBuilder.aggregation(aggregation);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        long total = hits.getTotalHits().value;
        System.out.println("总数:" + total);
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        Terms goods_brands = (Terms) aggregationMap.get("goods_brands");
        List<? extends Terms.Bucket> buckets = goods_brands.getBuckets();
        Map<String, Object> map = new HashMap<>();
        for (Terms.Bucket bucket : buckets) {
            System.out.println(bucket.getKey());
            map.put(bucket.getKeyAsString(), bucket.getDocCount());
        }
        System.out.println(map);
    }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
7月前
|
运维 监控 Java
探索Elasticsearch在Java环境下的全文检索应用实践
【6月更文挑战第30天】在大数据背景下,Elasticsearch作为分布式搜索分析引擎,因其扩展性和易用性备受青睐。本文指导在Java环境中集成Elasticsearch,涉及安装配置、使用RestHighLevelClient连接、索引与文档操作,如创建索引、插入文档及全文检索查询。此外,还讨论了高级查询、性能优化和故障排查,帮助开发者高效处理非结构化数据全文检索。
191 0
|
4月前
|
机器学习/深度学习 存储 监控
Elasticsearch 在日志分析中的应用
【9月更文第2天】随着数字化转型的推进,日志数据的重要性日益凸显。日志不仅记录了系统的运行状态,还提供了宝贵的洞察,帮助企业改进产品质量、优化用户体验以及加强安全防护。Elasticsearch 作为一个分布式搜索和分析引擎,因其出色的性能和灵活性,成为了日志分析领域的首选工具之一。本文将探讨如何使用 Elasticsearch 作为日志分析平台的核心组件,并详细介绍 ELK(Elasticsearch, Logstash, Kibana)栈的搭建和配置流程。
390 4
|
2月前
|
存储 SQL 监控
|
2月前
|
自然语言处理 监控 数据可视化
|
3月前
|
人工智能
云端问道12期-构建基于Elasticsearch的企业级AI搜索应用陪跑班获奖名单公布啦!
云端问道12期-构建基于Elasticsearch的企业级AI搜索应用陪跑班获奖名单公布啦!
187 2
|
8月前
|
缓存 关系型数据库 Java
不要将数据库中的“分库分表”理论盲目应用到 Elasticsearch
不要将数据库中的“分库分表”理论盲目应用到 Elasticsearch
60 0
|
5月前
|
存储 自然语言处理 API
Elasticsearch 在语音识别领域的应用
【8月更文第28天】随着语音助手和智能设备的普及,语音识别技术已经成为人们日常生活中不可或缺的一部分。Elasticsearch 作为一种高性能的搜索和分析引擎,在语音识别领域可以发挥重要作用,尤其是在提供快速准确的语音搜索结果方面。本文将介绍如何利用 Elasticsearch 来增强语音识别系统的搜索能力,并通过示例代码展示具体实现。
109 0
|
5月前
|
机器学习/深度学习 自然语言处理 搜索推荐
构建智能搜索应用:Elasticsearch与自然语言处理的融合
【8月更文第28天】随着大数据和人工智能技术的发展,用户对搜索应用的需求已经从简单的关键词匹配转向了更加智能化、人性化的交互方式。本文将探讨如何利用Elasticsearch和自然语言处理(NLP)技术构建一个能够理解用户意图并提供精准搜索结果的智能搜索系统。
395 0
|
7月前
|
存储 监控 搜索推荐
大规模数据存储与检索:Java与Elasticsearch应用
大规模数据存储与检索:Java与Elasticsearch应用
|
6月前
|
存储 监控 搜索推荐
大规模数据存储与检索:Java与Elasticsearch应用
大规模数据存储与检索:Java与Elasticsearch应用