最新Java Elasticsearch 7.x(7.10)教程(二)-rest api使用

一、几个概念

1、Index Type Document

一般我们初学时会把这些与数据库进行对照方便理解

Index->Database

Type->Table (最新版本已经不使用Type了,所以很多人会奇怪为什么去掉了?ES并非和数据库是相同的,所以不要完全按数据库的方式来看ES)

Document->Row

2、倒排索引

参考此文:(一般我们从目录找到相应的文章为正向索引,如果从关键词索引找到对应的文章即倒排索引)

ES 索引解析(倒排索引 | 正排索引)​www.jianshu.com

二、几种Java调用ES方式

  • Using Transport Client
  • Using the RestClient
  • Using Spring Data Repositories
  • Rest API

三、Rest API Test

参考官方文档:

https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-index.html​www.elastic.co

#1、创建索引及document
#e.g. PUT /index名/_doc/document id
#最新版本已经没有type,且这里固定为_doc
PUT /index-test/_doc/1
{
  "id":"1",
  "name": "hello world",
  "code":11
}


#2、更新document(有两种方式:方式一与创建索引一致就会更新;方式二改成POST)
#e.g. POST /index名/_doc/document id
POST /index-test/_doc/1
{
  "id":"1",
  "name": "hello world",
  "code":12
}


#3、查询
#e.g. GET /index名/_search
#可以查询出所以索引及数据
#其中_score为匹配度
GET /index-test/_search
{
	"query": {
		"match_all": {}
	}
}

#4、模糊查询  关键词  match  match_phrase  match_phrase_prefix
#match 包含模糊匹配、单词匹配、短语匹配 e.g. 查询“hello w”有结果 w不是一个完全的单词或短语
#match_phrase  包含单词匹配、短语匹配   e.g. 查询“hello w”无结果
#match_phrase_prefix  包含单词匹配、短语匹配,同时增加在集合内通配符匹配  e.g. 查询“hello w”有结果
# match  match_phrase_prefix 区别在于 hello wadasas  match有结果,match_phrase_prefix无结果
GET /index-test/_search
{
	"query": {
		"match": {
		  "name": "hello wa"
		}
	}
}


#5、精确查询(精确是指精确到单词)
GET /index-test/_search
{
	"query": {
		"term": {
		  "name": "hello"
		}
	}
}
#6、精确查询,且使用多个单词(精确是指精确到单词)
GET /index-test/_search
{
	"query": {
		"terms": {
		  "name": ["hello","world"]
		}
	}
}


#7、增加中文分词ik
#支持2种分词模式ik_smart  ik_max_word
#测试分词效果  _analyze
GET /index-test/_analyze
{
  "analyzer": "ik_max_word",
  "text": "你世界好"
}



#8、排序与分页
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "age": "asc" }
  ],
  "from": 1,
  "size": 1
}



#9、词频统计
DELETE message_index
#创建索引数据结构
PUT message_index
{
   "mappings": {
       "properties":{
            "message": {
               "analyzer": "ik_smart",
                "type": "text",
                "fielddata":"true"
            }
        }
    }
}

#增加doc1
PUT /message_index/_doc/1
{
   "message":"沉溺于「轻易获得高成就感」的事情:有意无意地寻求用很小付出获得很大「回报」的偏方,哪怕回报是虚拟的"
 }
#增加doc2
PUT /message_index/_doc/2
{
    "message":"过度追求“短期回报”可以先思考这样一个问题:为什么玩王者荣耀沉溺我们总是停不下来回报"

 }
 
#增加doc3
PUT /message_index/_doc/3
{
   "message":"过度追求的努力无法带来超额的回报,就因此放弃了努力。这点在聪明人身上尤其明显。以前念本科的时候身在沉溺"
 }
 
#aggs为Aggregations(聚合)缩写
#size 10 为前10的统计结果
#默认热点降序出结果
POST /message_index/_search
{
   "size" : 0,  
    "aggs" : {   
        "messages" : {   
            "terms" : {   
               "size" : 10,
              "field" : "message"
            }  
        }
    }
}

最新Java Elasticsearch 7.10教程(汇总)

最新Java Elasticsearch 7.10教程(汇总)_玄明Hanko的博客-CSDN博客_elasticsearch 7.10

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

栈江湖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值