当前位置:网站首页>Es restful operation
Es restful operation
2022-07-19 09:49:00 【Program three two lines】
One 、restful Style Introduction

Two 、 Index addition, deletion, modification and query
1、 Add index
command :put The index name / The document type / file id
PUT test/user/1
{
"name":" Zhang San ",
"age": 12,
"dec": " Test it "
}Fields can also define their own types If you want to add a field type , The operation is as follows

Common types

explain :
1、put The index name / The document type / file id The document type in 8 Version obsolete The default is _doc type
2、 stay es 2.* There are no these two fields in the version , Only string Field .5.* after , hold string Fields are set to obsolete fields , introduce text,keyword Field
Both of these fields can be used to store strings , But indexing and searching are different
keyword: When storing data , No word segmentation for indexing
text: When storing data , Automatic word segmentation , And generate an index ( This is very intelligent , But it's useless in some fields , So for some fields, use text It wastes space ).
Strings of all text types can be defined as “text” or “keyword” Two types of . The difference lies in ,text Type will use the default participator to segment words , Of course, you can also specify a specific word breaker for him . If it's defined as keyword type , Then it will not be segmented by default .
2、 Inquire about
command GET Index name
3、 modify
The way 1: Use the add command to overwrite the original content
The way 2:
POST Indexes / The document type / file id/_update
POST test/user/2/_update
{
"doc":{
"name":" Li Si 2",
"age": 12,
"dec": " Test it ",
"tags":[" Sing a song ","rap"," tourism "]
}
}4、 Delete
adopt DELETE The command determines whether the index or document record is deleted
3、 ... and 、 The document operation
1、 Add document
PUT test/user/1
{
"name":" Zhang San ",
"age": 12,
"dec": " Test it "
}
PUT test/user/2
{
"name":" Li Si ",
"age": 12,
"dec": " Test it ",
"tags":[" Sing a song ","rap"," tourism "]
}2、 Modify the document
//put Way update Every value will be left blank
PUT test/user/2
{
"name":" Li Si "
}
// recommend post _update to update
POST test/user/2/_update
{
"doc":{
"name":" Li Si 2",
"age": 12,
"dec": " Test it ",
"tags":[" Sing a song ","rap"," tourism "]
}
}3、 Delete the document
DELETE test/user/1
4、 Query the document
adopt id Inquire about
GET test/user/2
Conditions of the query
GET test/user/_search?q=name: Zhang San
Complex operation query
1、 Fuzzy matching query
GET test/user/_search
{
"query":{
"match": {
"name": " Zhang San "
}
}
}result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.847103,
"hits" : [
{
"_index" : "test",
"_type" : "user",
"_id" : "3",
"_score" : 0.847103,
"_source" : {
"name" : " Zhang Sansan ",
"age" : 12,
"dec" : " Test it "
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "1",
"_score" : 0.8259841,
"_source" : {
"name" : " Zhang San ",
"age" : 12,
"dec" : " Test it "
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "4",
"_score" : 0.62774795,
"_source" : {
"name" : " Zhang Sanxue java",
"age" : 12,
"dec" : " Test it "
}
}
]
}
}
hist Is the information of indexes and documents , It's a json, adopt java You can traverse the query related information
2、 Query the specified field, that is, field filtering
Query only name
GET test/user/_search
{
"query":{
"match": {
"name": " Zhang San "
}
},
"_source":["name"]
}result
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.847103,
"hits" : [
{
"_index" : "test",
"_type" : "user",
"_id" : "3",
"_score" : 0.847103,
"_source" : {
"name" : " Zhang Sansan "
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "1",
"_score" : 0.8259841,
"_source" : {
"name" : " Zhang San "
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "4",
"_score" : 0.62774795,
"_source" : {
"name" : " Zhang Sanxue java"
}
}
]
}
}3、 Sorting results
GET test/user/_search
{
"query":{
"match": {
"name": " Zhang San "
}
},
"sort":[
{
"age":{
"order":"desc"
}
}
]
}4、 Paging narration
from What page begins size How many pieces of data are returned
GET test/user/_search
{
"query":{
"match": {
"name": " Zhang San "
}
},
"from":0,
"size":3
}explain
The data subscript is still from 0 At the beginning
5、 Boolean query
must All conditions are similar and
should similar or
must_not similar not
GET test/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": " Zhang San "
}
},
{
"match": {
"age": 22
}
}
]
}
}
}6、 Use filte Filter
GET test/user/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"name": " Zhang San "
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 18
}
}
}
]
}
}
}gt Greater than
lt Less than
gte Greater than or equal to
lte Less than or equal to
7、 Match multiple conditions
GET test/user/_search
{
"query":{
"match": {
"tags": " Sing a song rap"
}
}
}Multiple conditions are separated by spaces , As long as one of them is satisfied, you can query
8、term Precise query
1. term&match
- term: Precise query , There is no word segmentation for the value of the query , Go straight into the inverted index to match .
- match; Fuzzy query , Word segmentation for the value of the query , The results of the segmentation one by one into the inverted index to match
2. text&keyword
- text: when written , Word segmentation of the written value , And then insert one by one into the inverted index .
- keyword: when written , Insert the entire value into the inverted index , No participle .
3. The example analysis
- The write value is hello world,
- The query value is hello world
Query type | Write type | result |
term | text | nothing |
term | keyword | Yes |
match | text | Yes |
match | keyword | Yes |
// Create index
PUT /test
{
"mappings": {
"properties": {
"name":{
"type": "keyword"
},
"desc":{
"type": "text"
}
}
}
}
// Add data
PUT test/_doc/1
{
"name":" test java A no. ",
"desc":" test java A no. "
}
//name yes keyword Type of You can accurately match and query
GET test/_search
{
"query": {
"term": {
"name": " test java A no. "
}
}
}
//desc yes text The type cannot exactly match the query
GET test/_search
{
"query": {
"term": {
"desc":" test java A no. "
}
}
}
9、 Precise query with multiple values matching

10、 Highlight query
The default is em label
GET test/_search
{
"query": {
"match": {
"desc": " test "
}
},
"highlight": {
"fields": {
"desc": {}
}
}
}
How to customize labels
GET test/_search
{
"query": {
"match": {
"desc": " test "
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"desc": {}
}
}
}
边栏推荐
猜你喜欢

C51 常见数据类型详解

研究发现DNA纳米设备注射液可安全用于医疗用途

中国十大国民小吃,第一居然是它

开发第一个Flink应用

On the problem of dependency invalidation when the dependency in the basic module is inherited by the sub module in the microservice

MySQL view

es索引、类型(mapping)、文档、ik分词器

Chapter 4 - consistency of first-order multi-agent systems - > consistency of continuous time systems with time delays

Part I - Fundamentals of C language_ 4. Procedure flow structure

第4章-一阶多智体系统一致性 -> 领航跟随系统一致性
随机推荐
Mux256to1v,Hadd,Fadd
Chapter 4 - first order multi-agent system consistency - > pilot follower system consistency
关于基础模块中的依赖由微服务中的子模块继承的时候依赖失效的问题
实验1:使用Matlab工具箱进行相机标定实验
v-mode
CLWY权限管理(二)--- 用户模块
第4章-一阶多智体系统一致性 -> 连续时间含时延系统一致性
第一部分—C语言基础篇_6. 函数
【C语言】整形数据的存储
Week 1: introduction to deep learning and foundation of pytorch
Pytorch calls cublasltmattmul to do GEMM and add bias. It's well written
Questions d'entrevue - concevoir des cas d'essai pour:: memcpy
Have you learned the database design pattern of multi tenant SaaS?
开发第一个Flink应用
Anaconda and jupyter notebook entry level detailed tutorial
第一部分—C语言基础篇_3. 运算符与表达式
[performance optimization methodology series] VI. summary
Chapter 4 - first order multi-agent system consistency - > switching topology system consistency
Fundamentals of C language -- 2-3 pointers and arrays
Chapter 13 set/ multiset of STL