Elasticsearch搜索引擎 - Springboot+ElasticSearchTemplate使用方法总结和用Postman操作ES

  • ElasticsearchTemplate6.x + Springboot2.x
  • Postman

目录

ElasticSearch集成Spring之ElasticsearchTemplate

maven依赖

spring-data-elasticsearch与elasticsearch的版本对应关系:

elasticsearchTemplate

引用

创建索引

插入单条数据

插入多条数据

单字符串查询

排序

模糊查询

其余匹配

Term全等查询

GetQuery根据id查询

RangeQuery范围查询

组合查询

ElasticSearch操作-Postman

创建索引:

查看索引下的数据:

查询所有索引:

删除索引:

ik分词索引:

查看索引数据结构

插入数据 

ElasticSearch集成Spring之ElasticsearchTemplate

maven依赖

<!-- elasticsearch的spring-data包 -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>x.y.z.RELEASE</version>
</dependency>

<!-- elasticsearch客户端,version填入本地安装好的ES版本号 -->
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>transport</artifactId>
	<version>x.y.z</version>
</dependency>

spring-data-elasticsearch与elasticsearch的版本对应关系:

spring data elasticsearchelasticsearch
3.2.x 6.5.0
3.1.x6.2.2
3.0.x5.5.0
2.1.x2.4.0
2.0.x2.2.0
1.3.x1.5.2
 

补充: 

                         :   

  文档:

elasticsearchTemplate

引用

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

创建索引

// 1、直接用名称创建
elasticsearchTemplate.createIndex("book");

// 2、填入class对象
elasticsearchTemplate.createIndex(BookEntity.class);

插入单条数据

BookEntity bookEntity = new BookEntity();
bookEntity.setId("2");
bookEntity.setAuthor("老李");
bookEntity.setName("三年高考");

IndexQuery indexQuery = new IndexQueryBuilder()
				.withId(bookEntity.getId())
				.withObject(bookEntity)
				.build();
elasticsearchTemplate.index(indexQuery);

插入多条数据

BookEntity bookEntity = new BookEntity();
bookEntity.setId("1");
bookEntity.setAuthor("测试1");
bookEntity.setName("测试1");

IndexQuery indexQuery1 = new IndexQueryBuilder()
		.withId(bookEntity.getId())
		.withObject(bookEntity)
		.build();
//		elasticsearchTemplate.index(indexQuery);
indexQueries.add(indexQuery1);

BookEntity bookEntity2 = new BookEntity();
bookEntity.setId("2");
bookEntity.setAuthor("测试2");
bookEntity.setName("测试2");

IndexQuery indexQuery2 = new IndexQueryBuilder()
		.withId(bookEntity2.getId())
		.withObject(bookEntity)
		.build();
indexQueries.add(indexQuery2);

elasticsearchTemplate.bulkIndex(indexQueries);

单字符串查询

SearchQuery searchQuery = new NativeSearchQueryBuilder()
		.withQuery(QueryBuilders.queryStringQuery("菜鸟"))
		.withPageable(new PageRequest(0, 20))
		.build();
List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);

排序

Pageable pageable= new PageRequest(0, 20,new Sort(Sort.Direction.DESC, "name"));
SearchQuery searchQuery = new NativeSearchQueryBuilder()
		.withQuery(QueryBuilders.queryStringQuery("菜鸟"))
		.withPageable(pageable)
		.build();
Page<BookEntity> list = elasticsearchTemplate.queryForPage(searchQuery, BookEntity.class);

模糊查询

Pageable pageable = new PageRequest(0, 10);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
		.withQuery(QueryBuilders.matchQuery("name", "菜鸟"))
		.withPageable(pageable)
		.build();
List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);

其余匹配

Pageable pageable = new PageRequest(0, 10);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
		.withQuery(QueryBuilders.matchPhraseQuery("name", "菜鸟"))
		.withPageable(pageable)
		.build();
List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);

Term全等查询

Pageable pageable = new PageRequest(0, 10);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
		.withQuery(QueryBuilders.termQuery("name", "菜鸟"))
		.withPageable(pageable)
		.build();
List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);

GetQuery根据id查询

GetQuery getQuery = GetQuery.getById(String.valueOf(admin.getId()));
Admin admin = template.queryForObject(getQuery, Admin.class);

RangeQuery范围查询

  • 对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRangeQuery
  • 范围查询接受以下参数:
    • gte:  大于或等于
    • gt:   大于
    • lte:  小于或等于
    • lt:     小于
    • boost:  设置查询的提升值,默认为1.0
    • format(String format)  对于日期字段,我们可以设置要使用的格式,而不是映射器格式
    • timeZone      时区
    • from       范围查询的from部分
RangeQueryBuilder createTime = QueryBuilders.rangeQuery("createTime").from(questions.getStartTime()).to(questions.getEndTime());
            boolQueryBuilder.should(createTime);

组合查询

boolQuery,可以设置多个条件的查询方式。它的作用是用来组合多个Query,有四种方式来组合,must,mustnot,filter,should。
must代表返回的文档必须满足must子句的条件,会参与计算分值;
filter代表返回的文档必须满足filter子句的条件,但不会参与计算分值;
should代表返回的文档可能满足should子句的条件,也可能不满足,有多个should时满足任何一个就可以,通过minimum_should_match设置至少满足几个。
mustnot代表必须不满足子句的条件。

QueryBuilder filterQuery = QueryBuilders
				.boolQuery()
				.filter(QueryBuilders.termQuery("name", "菜鸟"))
				.filter(QueryBuilders.termQuery("author", "小菜"));
List<BookEntity> list = elasticsearchTemplate.queryForList(filterQuery, BookEntity.class);

组合查询举例:should

NativeSearchQueryBuilder query = new NativeSearchQueryBuilder();

TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", "b31ea3a48f0f48738ecbc4a25534d6c6");
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "你好");

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(matchQueryBuilder); // or
boolQueryBuilder.should(termQueryBuilder);

query.withQuery(boolQueryBuilder);

ElasticSearch操作-Postman

创建索引:

PUT http://ip:port/indexname

查看索引下的数据:

POST  http://ip:port/indexname/indextype/_search?pretty

查询所有索引:

GET http://ip:port/_cat/indices?v

删除索引:

DELETE http://ip:port/indexname

删除多个:/index_one,index_two       /index_*

ik分词索引:

POST http://ip:port/user_index/_analyze

查看索引数据结构

GET http://ip:port/index_name/_mapping

插入数据 

POST http://ip:port/index/type/id

http://192.168.0.148:9200/user_index/user/9ebcb330919b4baa9403a7564cfb1e4a

删除索引下所有数据

POST http://0.0.0.0:9200/索引/标签/_delete_by_query?pretty

{
    "query": {
        "match_all": {}
    }
}

其他用到再补充


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑶山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值