当前位置: 代码迷 >> 综合 >> ElasticSearch高级操作---javaAPI
  详细解决方案

ElasticSearch高级操作---javaAPI

热度:76   发布时间:2024-02-24 19:33:16.0

批量操作

Bulk 批量操作是将文档的增删改查一些列操作,通过一次请求全都做完。减少网络传输次数。

脚本操作:

POST _bulk
{
    "delete":{
    "_index":"person","_id":"4"}}
{
    "create":{
    "_index":"person","_id":"4"}}
{
    "name":"五号","age":20,"address":"北京"}
{
    "update":{
    "_index":"person","_id":"2"}}
{
    "doc":{
    "name":"二号"}}

删除 索引为person的索引中id为4的文档
在索引为 person 的索引中创建id为4的文档,内容为下面一行的内容。
更新 索引为 person的索引中的id为2的文档,更新的内容在下面一行,文档类型为doc,将name对应的值修改为二号

一条json格式的脚本代码只能写在一行

每一条脚本代码相互之间是独立,互相不干扰。

基于SpringBoot的JavaAPI操作

/*** 1. 批量操作 bulk*/
@Test
public void testBulk() throws IOException {
    //创建bulkrequest对象,整合所有操作BulkRequest bulkRequest = new BulkRequest();/*# 1. 删除1号记录# 2. 添加6号记录# 3. 修改3号记录 名称为 “三号”*///添加对应操作//1. 删除1号记录DeleteRequest deleteRequest = new DeleteRequest("person","1");bulkRequest.add(deleteRequest);//2. 添加6号记录Map map = new HashMap();map.put("name","六号");IndexRequest indexRequest = new IndexRequest("person").id("6").source(map);bulkRequest.add(indexRequest);Map map2 = new HashMap();map2.put("name","三号");//3. 修改3号记录 名称为 “三号”UpdateRequest updateReqeust = new UpdateRequest("person","3").doc(map2);bulkRequest.add(updateReqeust);//执行批量操作BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);RestStatus status = response.status();System.out.println(status);}

将MySql中的数据导入到Elastic Search中

/** * 批量导入,数据库查询出来的spec字段是字符串格式的json数据, */
@Test
public void importData() throws IOException {
    //1.查询所有数据,mysqlList<Goods> goodsList = goodsMapper.findAll();//System.out.println(goodsList.size());//2.bulk导入BulkRequest bulkRequest = new BulkRequest();//2.1 循环goodsList,创建IndexRequest添加数据for (Goods goods : goodsList) {
    //2.2 设置spec规格信息 Map的数据 specStr:{}//goods.setSpec(JSON.parseObject(goods.getSpecStr(),Map.class));String specStr = goods.getSpecStr();//将json格式字符串转为Map集合Map map = JSON.parseObject(specStr, Map.class);//设置spec mapgoods.setSpec(map);//将goods对象转换为json字符串String data = JSON.toJSONString(goods);//map --> {}IndexRequest indexRequest = new IndexRequest("goods");indexRequest.id(goods.getId()+"").source(data, XContentType.JSON);bulkRequest.add(indexRequest);}BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println(response.status());}

Goods类,省略getter和setter及其构造方法

public class Goods {
    private int id;private String title;private double price;private int stock;private int saleNum;private Date createTime;private String categoryName;private String brandName;private Map spec;@JSONField(serialize = false)//在转换JSON时,忽略该字段private String specStr;//接收数据库的信息 "{}"}

matchALl查询----查询所有文档

脚本操作

语法:

GET 索引名称/_search
{
    "query":{
    "match_all":{
    }}
}

默认情况下,es一次展示十条数据

GET 索引名称/_search
{
    "query":{
    "match_all":{
    }},"form":0,"size":100
}

通过 from 从哪儿开始 size 每页多少条

查询信息解读:

{
    "took" : 1,// 耗时(毫秒)"timed_out" : false, //是否超时"_shards" : {
    "total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {
    // 查询的结果集"total" : {
    // 总数"value" : 1,"relation" : "eq"},"max_score" : 1.0,//最大评分"hits" : [// 数据列表{
    "_index" : "test2","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {
    // 它是数据document的json内容"name" : "张三","age" : "1"}}]}
}

JavaAPI操作:

@Test
public void testMatchAll() throws IOException {
    //2. 构建查询请求对象,指定查询的索引名称SearchRequest searchRequest = new SearchRequest("goods");//4. 创建查询条件构建器SearchSourceBuilderSearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//6. 查询条件QueryBuilder query = QueryBuilders.matchAllQuery();//查询所有文档//5. 指定查询条件sourceBuilder.query(query);//3. 添加查询条件构建器 SearchSourceBuildersearchRequest.source(sourceBuilder);// 8 . 添加分页信息sourceBuilder.from(0);sourceBuilder.size(100);//1. 查询,获取查询结果SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);//7. 获取命中对象 SearchHitsSearchHits searchHits = searchResponse.getHits();//7.1 获取总记录数long value = searchHits.getTotalHits().value;System.out.println("总记录数:"+value);List<Goods> goodsList = new ArrayList<>();//7.2 获取Hits数据 数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {
    //获取json字符串格式的数据String sourceAsString = hit.getSourceAsString();//转为java对象Goods goods = JSON.parseObject(sourceAsString, Goods.class);goodsList.add(goods);}for (Goods goods : goodsList) {
    System.out.println(goods);}
}

Term:精确查询

搜索关键字不分词与字段内容完全匹配,就是等号

GET _search
{
    "query": {
    "term": {
    "name": {
    "value": "张三"}}}
}

后面的查询都只需要在上面matchAll的代码上更改query即可

/*** termQuery:词条查询*/
@Test
public void testTermQuery() throws IOException {
    SearchRequest searchRequest = new SearchRequest("goods");SearchSourceBuilder sourceBulider = new SearchSourceBuilder();QueryBuilder query = QueryBuilders.termQuery("title","华为");//term词条查询sourceBulider.query(query);searchRequest.source(sourceBulider);SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);SearchHits searchHits = searchResponse.getHits();//获取记录数long value = searchHits.getTotalHits().value;System.out.println("总记录数:"+value);List<Goods> goodsList = new ArrayList<>();SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {
    String sourceAsString = hit.getSourceAsString();//转为javaGoods goods = JSON.parseObject(sourceAsString, Goods.class);goodsList.add(goods);}for (Goods goods : goodsList) {
    System.out.println(goods);}
}

Match:模糊查询

分词之后,可以求并集或者交集

// A:1、2、3   B:1、2、5
// operator:or并集=1、2、3、5
// operator:and交集=1、2
GET _search
{
    "query": {
    "match": {
    "name": {
    "query": "张三","operator": "or"}}}
}

/*** matchQuery:词条分词查询*/
@Test
public void testMatchQuery() throws IOException {
    SearchRequest searchRequest = new SearchRequest("goods");SearchSourceBuilder sourceBulider = new SearchSourceBuilder();MatchQueryBuilder query = QueryBuilders.matchQuery("title", "华为手机");query.operator(Operator.AND);//求并集sourceBulider.query(query);searchRequest.source(sourceBulider);SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);SearchHits searchHits = searchResponse.getHits();//获取记录数long value = searchHits.getTotalHits().value;System.out.println("总记录数:"+value);List<Goods> goodsList = new ArrayList<>();SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {
    String sourceAsString = hit.getSourceAsString();//转为javaGoods goods = JSON.parseObject(sourceAsString, Goods.class);goodsList.add(goods);}for (Goods goods : goodsList) {
    System.out.println(goods);}
}

Wildcard:通配符查询

就是like查询,?是单个字符,*是任意字符。注意竟可能星号不要写在前面

GET _search
{"query": {"wildcard": {"name": {"value": "张?"}}}
}
 WildcardQueryBuilder query = QueryBuilders.wildcardQuery("name", "张?");

Regexp:正则查询

查询的字段使用正则表达式

GET _search
{"query": {"regexp": {"name": "[张](.)*"}}
}
RegexpQueryBuilder query = QueryBuilders.regexpQuery("name", "张(.)*");

Prefix:前缀查询

查询以某个关键词开头的数据

GET _search
{"query": {"prefix": {"name": "张"}}
}
PrefixQueryBuilder query = QueryBuilders.prefixQuery("name", "张");

Range:范围查询

GET _search
{"query": {"range": {"age": {"gte": 1,"lte": 20}}}
}
RangeQueryBuilder query = QueryBuilders.rangeQuery("age").gte(1).lte(10);

QueryString:表达式查询

  • 对多个字段和条件进行组合查询,并默认取交集
  • 对于输入的关键字会进行分词
GET _search
{"query": {"query_string": {"query": "name:张二 AND age:1"}}
}
QueryStringQueryBuilder query = QueryBuilders.queryStringQuery("name:张二 AND age:1");

SimpleQueryString:多字段条件查询

  • 对多个字段查询
  • 对于输入的关键字不会进行分词
GET _search
{"query": {"simple_query_string": {"fields": ["name","age"], "query": "name:张二 AND age:1"}}
}

Bool:组合条件查询

只组合其它搜索条件,无自己的特殊条件

  • 组合方式
    • must:执行查询,并计算评分
    • filter:执行查询,不计算评分
GET _search
{"query": {"bool": {"must": [{"term": {"name": {"value": "张三"}}}],"filter": {"range": {"age": {"gte": 1,"lte": 20}}}}}
}

javaAPI

TermQueryBuilder term = QueryBuilders.termQuery("name","张三");WildcardQueryBuilder wildcard = QueryBuilders.wildcardQuery("name", "张?");BoolQueryBuilder query = QueryBuilders.boolQuery();query.must(term);query.filter(wildcard);builder.query(query);
  相关解决方案