elasticsearch7.X 创建索引、文档、fields

输入图片说明

一、索引的增删改查
rest 一定要大写哦哈哈
增加索引 : customer 索引名,_doc  type类型 ,1 文档id 大括号中的为 请求体(及数据)
1、创建索引
PUT /索引名/类型名(会删除)/文档id  
{
    请求体
}
说明:PUT手动指定索引
  
例:创建索引
PUT /customer/_doc/1
{
  "name": "John Doe"
}
2、查询索引
GET /customer/_doc/1
3、删除索引
DELETE /customer

4、修改
PUT /customer/_doc/1 {
    "name": "大帅逼"
}
说明:这种方式会将version版本号增加,但是添加时需要与前面文档中数据数目一致不然会丢失数据

POST  /test20210304/_doc/1/_update
{
    "doc":{
      "name": "大帅逼"
    }
}
二、索引的属性修改
创建索引规则,就像设置数据库的一些参数一样,
PUT /test1
{
  "mappings": {
    "properties": {
      "name": {
        "type":"text"
      }
    }
  }
}

说明:设置文档中对应词组的类型

GET /test1  查看索引的信息

三、查看elasticsearch状态
GET _cat/   获取es的信息状态

四、文档的操作

1、基本操作
1)添加数据

PUT /testbasedoc/_doc/1
{
  "name":"大帅比大水笔",
  "age":"23",
  "desc":"喜之郎果冻,盼盼我爱你",
  "likes":["java 爪哇","可爱","刘月半"]
  
}
PUT /testbasedoc/_doc/2
{
  "name":"鸡蛋",
  "age":"23",
  "desc":"喜之冻,牙医媒体",
  "likes":["电影ae,ar","胖胖","倩妹子"]
}
PUT /testbasedoc/_doc/3
{
  "name":"鸭蛋",
  "age":"23",
  "desc":"兴趣使然的理发师",
  "likes":["打游戏","剪头发","妹子"]
} 

2)查询
GET testbasedoc/_doc/1

3)更新

POST  /test20210304/_doc/1/_update
{
    "doc":{
      "name": "大帅比2"
    }
}

4)删除文档
DELETE /tehero_index/_doc/1

5)批量操作文档
a、查询多个_mget

POST /_mget
{
   "docs":[
      {
         "_index": "testbasedoc", "_id": "1"
      },
      {
         "_index":"testbasedoc", "_id": "2"
      }
   ]
}

b、批量执行多个操作

POST _bulk
## 替换指定文档
{ "index" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
{ "this_is_field1" : "this_is_index_value" }
##删除指定文档
{ "delete" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
##创建文档
{ "create" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
{   "name":"大帅比大水笔","age":"23","desc":"喜之郎果冻,盼盼我爱你","likes":["java 爪哇","可爱","刘月半"]}
##更新文档
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "testbasedoc"} }
{ "doc" : {"age" : "24"} }

在7.X中也可以去掉"_type" : "_doc" 

2、复杂操作 (排序,分页,高亮,模糊查询,精准查询)

1)简单搜索_search?q=name:大帅比java

a、GET testbasedoc/_doc/_search?q=name:大帅比jav
b、GET /testbasedoc,customer/_search?q=name:"wenye" 多索引查询统一条件

GET testbasedoc/_doc/_search
{
  "query": {
    "match": {
      "name":"大帅比"
    }
  }
}

查询结果
输入图片说明

2)聚合查询 metric,bucket

metric:avf 、min、max、cardinality、sum、stats等
Bucket:想当与group by
1)平均值
POST  /testbasedoc/_search
{
   "aggs":{
      "avg_age":{"avg":{"field":"age"}}
   }
}

2)计数

POST /schools*/_search
{
   "aggs":{
      "distinct_name_count":{"cardinality":{"field":"name"}}
   }
}

3)统计

POST  /schools/_search
{
   "aggs" : {
      "fees_stats" : { "stats" : { "field" : "fees" } }
   }
}

4) 最大聚合

POST /schools*/_search
{
   "aggs" : {
      "max_fees" : { "max" : { "field" : "fees" } }
   }
}

5) 最小聚合

POST /schools*/_search
{
   "aggs" : {
      "min_fees" : { "min" : { "field" : "fees" } }
   }
}

6)特定字段的总和

POST /schools*/_search
{
   "aggs" : {
      "total_fees" : { "sum" : { "field" : "fees" } }
   }
}

7)terms 桶 精确查询 根据倒排索引进行精确索引


GET /testbasedoc1/_search
{
  "aggs": {                    # 使用聚合的必须带如设置_mapping 必须以properties开头
    "boyfirl_term": {
      "terms": {
        "field": "age"
      },
      "aggs" :{
        "avg_age":{
          "avg":{"field":"age"}
        }
      }
    }
  }
}

3、搜索相关


#搜索
#简单搜索 match_all
POST /testbasedoc1/_search
{
  "query": {
    "match_all": {}
  }
, "_source": ["name","age"] # 指定显示的字段
}

#分页查询
POST /testbasedoc1/_search
{
  "from":0,     #哪里开始
  "size": 2,   #查询出的总个数
  "query": {
    "match_all": {}
  }
}

#排序
POST /testbasedoc1/_search
{
  "sort":[{"age":"asc"}],
  "from":0,     
  "size": 20,   
  "query": {
    "match_all": {}
  }
}

#match 匹配查询
POST /testbasedoc1/_search
{
  "query": {
    "match": {
      "age": 23
    }
  }
}

#(multi_mathc)多种匹配查询

POST /testbasedoc1/_search
{
  "query": {
    "multi_match": {
      "query": "23",  #value
      "fields": ["name","age"] #column 数组
    }
  }
}

#布尔查询
POST /testbasedoc1/_search
{
  "query": { 
   "bool":{     #返回的会是boolean值
     "must":[   #must 必须相等 must_not 不相等 should (or)
        { 
          "match":{
            "name":"大帅比"
          }
        },
       {
         "match":
         {
           "age":"23"
          }
       }
     ]
   }
  }
}

#过滤
POST /testbasedoc1/_search
{
  "query": {
     "bool":{
       "must":[  
        { 
          "match":{
            "name":"鸭蛋"
          }
        },
       {
         "match":
         {
           "age":"24"
          }
       }
     ],
     "filter": [  #过滤
       {
         "range": {
           "FIELD": {
             "gte": 10,
             "lte": 20
           }
         }
       }
     ]
   }
  }
}
#query :查询包含str的文档

POST /testbasedoc1/_search
{
   "query":{
      "query_string":{
         "query":"爱"
      }
   }
}

#短语搜索 - Match Phrase  搞不清
POST testbasedoc1/_search
{
  "query": {
    "match_phrase": {
      "desc":{
        "query": "兴趣使,然理发师"
        , "slop": 1
      }
    }
  }
}
#term 查询 主要用于数字日期相关
POST /testbasedoc/_search
{
   "query":{
      "term":{"age":"23"}
   }
}

#range 查询范围查询

POST /testbasedoc*/_search
{
   "query":{
      "range":{
         "age":{
            "gte":3.5   #gte:大于等于  gt:大于 lte:小于等于  lt:小于
         }
      }
   }
}

4、关于分词

  • term : 精确查询,不会对词组进行解析
  • match: 先进行文档解析,然后进行匹配
  • 类型为keyword的不会被分词器解析

5、高亮显示

默认:
POST /testbasedoc/_search
{
   "query":{
      "match":{"name":"大帅比"}
   },
   "highlight": {
     "fields": {
       "name": {}
     }
   }
}

自定义 高亮条件 pre_tags post_tages:
POST /testbasedoc/_search
{
   "query":{
      "match":{"name":"大帅比"}
   },
   "highlight": {
     "pre_tags":"

", "post_tags":"

", "fields": { "name": {} } } } 匹配的结果 "hits" : [ { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1", "_score" : 2.0487494, "_source" : { "name" : "大帅比大水笔", "age" : 23, "desc" : "喜之郎果冻,盼盼我爱你", "likes" : [ "java 爪哇", "可爱", "刘月半" ] }, "highlight" : { "name" : [ "大水笔" 高亮 ]

你可能感兴趣的:(elasticsearch)