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); } }