【自学elasticsearch7】结合es语法和java的HighLevelClient:es最基本的增删改

【自学elasticsearch7】结合es语法和java的HighLevelClient:es最基本的增删改,第1张

【自学elasticsearch7】结合es语法和java的HighLevelClient:es最基本的增删改

【自学elasticsearch7】结合es语法和java的HighLevelClient:es最基本的增删改
  • 写在开头
    • 增:索引文档
    • 删除文档
    • 修改文档

写在开头

es作为一个分布式搜索引擎,最重要的就是它强大的查询功能,我们使用es也是为了它的查询功能,但是在学习es的查询之前也必须先去学习如何在es中索引文档、删除文档和修改文档,在这之前,也有必要先了解es的索引分析功能,即_analyze。

书接上回说道,我们创建了firstmapping这个映射,我们用_analyze命令试一下该索引的分词功能:

在kibana中输入以下内容:

GET /firstmapping/_analyze
{
  "field": "content",
  "text": "I love you three thousand times"
}

content字段创建映射时是text类型,es将其按照英文单词进行分词可以得到以下结果,其中token为分词结果:

{
  "tokens" : [
    {
      "token" : "i",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "love",
      "start_offset" : 2,
      "end_offset" : 6,
      "type" : "",
      "position" : 1
    },
    {
      "token" : "you",
      "start_offset" : 7,
      "end_offset" : 10,
      "type" : "",
      "position" : 2
    },
    {
      "token" : "three",
      "start_offset" : 11,
      "end_offset" : 16,
      "type" : "",
      "position" : 3
    },
    {
      "token" : "thousand",
      "start_offset" : 17,
      "end_offset" : 25,
      "type" : "",
      "position" : 4
    },
    {
      "token" : "times",
      "start_offset" : 26,
      "end_offset" : 31,
      "type" : "",
      "position" : 5
    }
  ]
}

而当我在kibana中输入以下内容,试图对title字段进行分词时会失败:

GET /firstmapping/_analyze
{
  "field": "title",
  "text": "I love you three thousand times"
}

因为title是keyword类型,es不对该类型字段进行分词分析,所以得到以下结果:

{
  "tokens" : [
    {
      "token" : "I love you three thousand times",
      "start_offset" : 0,
      "end_offset" : 31,
      "type" : "word",
      "position" : 0
    }
  ]
}

使用_analyze可以验证自定义分词器是否配置成功。

增:索引文档

1.索引一个文档
kibana的 *** 作方式,向firstmapping这个索引下添加一个文档:

POST /firstmapping/_doc
{
  "title":"The old man and the sea",
  "content":"作者海明威,讲述一个老人与鲨鱼搏斗的故事",
  "score":"8.8"
}

执行 GET /firstmapping/_search 查看结果:

{
  "took" : 205,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "qty6t30ByJCT9OD2tHyp",
        "_score" : 1.0,
        "_source" : {
          "title" : "The old man and the sea",
          "content" : "作者海明威,讲述一个老人与鲨鱼搏斗的故事",
          "score" : "8.8"
        }
      }
    ]
  }
}

java中索引一个文档:

  HashMap doc = new HashMap();
  doc.put("title", "战争与和平");
  doc.put("content", "一本好书");
  doc.put("score", "7.8");
  IndexRequest indexRequest = new IndexRequest("firstmapping");
        try {
            String docId = "12138"; 
            indexRequest
            	.id(docId)   //自定义文档的id,若不填写则自动生成
            	.source(doc);
            client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

kibana执行 GET /firstmapping/_search 查看结果:

{
  "took" : 628,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "qty6t30ByJCT9OD2tHyp",
        "_score" : 1.0,
        "_source" : {
          "title" : "The old man and the sea",
          "content" : "作者海明威,讲述一个老人与鲨鱼搏斗的故事",
          "score" : "8.8"
        }
      },
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "12138",
        "_score" : 1.0,
        "_source" : {
          "title" : "战争与和平",
          "content" : "一本好书",
          "score" : "7.8"
        }
      }
    ]
  }
}

2.批量索引文档:
kibana:

POST /firstmapping/_bulk
{ "index": { "_id": 131453 }}
{ "title" : "五年编程三年脱发", "content" : "你脱不脱吧","score":"10.0" }
{ "index": { "_id": 125454 }}
{ "title" : "华强买瓜", "content" : "此时一个华强开着摩托车来买瓜......","score":"9.9" }

java:

BulkRequest request = new BulkRequest("firstmapping");
 HashMap doc = new HashMap();
  doc.put("title", "战争与和平");
  doc.put("content", "一本好书");
  doc.put("score", "7.8");
 HashMap doc2 = new HashMap();
  doc2.put("title", "战争与和平");
  doc2.put("content", "一本好书");
  doc2.put("score", "7.8");
request.add(new IndexRequest("firstmapping")
            .id("12345")
            .source(doc));
 request.add(new IndexRequest("firstmapping")
            .id("54321")
            .source(doc2));
 try {
     client.bulk(request, RequestOptions.DEFAULT);
 } catch (IOException e) {
     e.printStackTrace();
 }    
删除文档

1.根据文档id进行删除:
kibana:

DELETE /firstmapping/_doc/12345

java:

DeleteRequest deleteRequest = new DeleteRequest("firstmapping", "54321");//映射名和文档id
try {
     client.delete(deleteRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
     e.printStackTrace();
}

2.批量删除:根据查询条件进行搜索,这里举一例最简单的删除索引下全部文档:(慎用删除全部功能!!!)
kibana:

POST /firstmapping/_delete_by_query
{
  "query":
  {
    "match_all":{}
  }
}

java:

  DeleteByQueryRequest request = new DeleteByQueryRequest("firstmapping");
        QueryBuilder builder = QueryBuilders.matchAllQuery();
        request.setQuery(builder);
        try{
            client.deleteByQuery(request, RequestOptions.DEFAULT);
        }catch (IOException e){
           e.printStackTrace();
        }
修改文档

实际上es本身是不支持文档的修改的,es自带的修改功能本质是用新的文档替换掉旧的文档,此处还涉及到并发问题,es为文档提供了version字段,当多人同时提交修改时可以根据提交的修改version来进行判断是否可以修改。若提交的修改版本和es中的版本不一致即无法修改。

PUT /firstmapping/_doc/125454
{
  "title":"华强来买阿瓦达啃大瓜",
  "content":"此时一个摩托开着华强来买瓜......",
  "score":"10.0"
}

在响应中,我们可以看到Elasticsearch把_version和_seq_no增加了。
_version:文档的版本
_seq_no:当前映射的版本

{
  "_index" : "firstmapping",
  "_type" : "_doc",
  "_id" : "125454",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 6,
  "_primary_term" : 1
}

java中修改只需要再索引一次相同id的文档即可。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5677886.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存