ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

es restAPI 练习

2021-09-16 11:00:13  阅读:164  来源: 互联网

标签:search 12 name GET restAPI 练习 ems query es


#查看所有索引
GET /_cat/indices?v

#删除索引
DELETE /*


# 创建/book索引并创建(musicbook)类型  没有类型了 类型默认_doc
PUT /book
{
  "mappings": {
   
      "properties": {
        "name":{
          "type":"keyword"
        },
        "price":{
          "type":"double"
        },
        "desc":{
          "type":"text"
        }
      }
  }
}

#查看类型映射
GET /book/_mapping

#添加文档
PUT /book/_doc/2
{
  "name":"音乐书2",
  "price":"22.0",
  "desc":"这是一本音乐书"
}

# 在原有数据基础上进行更新 不加_update 则会舍弃之前的字段
POST /book/_doc/1/_update
{
  "doc": {
    "price":"40"
  }
}

GET /book/_doc/1

#查询练习

# 1.删除索引
DELETE /ems

# 2.创建索引并指定类型
PUT /ems
{
  "mappings":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"integer"
        },
        "bir":{
          "type":"date"
        },
        "content":{
          "type":"text"
        },
        "address":{
          "type":"keyword"
        }
    }
  }
}

# 3.插入测试数据
PUT /ems/_doc/_bulk
  {"index":{}}
  {"name":"小黑","age":23,"bir":"2012-12-12","content":"为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平","address":"北京"}
  {"index":{}}
  {"name":"王小黑","age":24,"bir":"2012-12-12","content":"Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式","address":"上海"}
  {"index":{}}
  {"name":"张小五","age":8,"bir":"2012-12-12","content":"Spring Cloud 作为Java 语言的微服务框架,它依赖于Spring Boot,有快速开发、持续交付和容易部署等特点。Spring Cloud 的组件非常多,涉及微服务的方方面面,井在开源社区Spring 和Netflix 、Pivotal 两大公司的推动下越来越完善","address":"无锡"}
  {"index":{}}
  {"name":"win7","age":9,"bir":"2012-12-12","content":"Spring的目标是致力于全方位的简化Java开发。 这势必引出更多的解释, Spring是如何简化Java开发的?","address":"南京"}
  {"index":{}}
  {"name":"梅超风","age":43,"bir":"2012-12-12","content":"Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API","address":"杭州"}
  {"index":{}}
  {"name":"张无忌","age":59,"bir":"2012-12-12","content":"ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口","address":"北京"}

#全部检索 
GET /ems/_search
{
  "query": {
    "match_all": {}
  }
}

#指定数据大小
GET /ems/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2
}
#分页查询
GET /ems/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5,
  "from": 0
}


# 展示单个字段
GET /ems/_search
{
  "query": {
    "match_all": {}
  },
  "_source": "name"
}


# 展示多个字段
GET /ems/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name","age"]
}


# name 是text类型,会进行分词,所以name包含“张”的文档都行
GET /ems/_search
{
  "query": {
    "term": {
      "name": "张"
    }
  }
}

# bir 是date类型,不会进行分词,所以按照整体查询,查不到数据
GET /ems/emp/_search
{
  "query": {
    "term": {
      "bir": "2012"
    }
  }
}

# 查询age>=5,<=10的文档
GET /ems/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 5,
        "lte": 10
      }
    }
  }
}

#前缀查询
GET /ems/_search
{
  "query": {
    "prefix": {
      "name": {
        "value": "无"
      }
    }
  }
}
#通配符查询
GET /ems/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "张*"
      }
    }
  }
}
#多id查询
GET /ems/_search
{
  "query": {
    "ids": {
      "values": ["AlSspHYBh-o7eO8i7bUf","BVSspHYBh-o7eO8i7bUf"]
    }
  }
}

#模糊查询 指的是 搜索的字如果打错了 一定范围内 还是可以查到
GET /ems/_search
{
  "query": {
      "fuzzy": {
        "content": "sprin"
      }
  }
}
#bool 关键字用来组合多个条件实现杂#查询相当于&& 同时成立相当于|| 
#成立一个就行相当于!#不能满足任何一个
GET /ems/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 5,
              "lte": 10
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "address": {
              "value": "南"
            }
          }
        }
      ]
    }
  }
}

#高亮查询
GET /ems/_search
{
  "query": {
    "term": {
      "name": {
        "value": "五"
      }
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"], 
    "post_tags": ["</span>"], 
    "fields": {
      "name":{}
    }
  }
}
#多字段查询
GET /ems/_search
{
  "query": {
    "multi_match": {
      "query": "中国",
      "fields": ["name","content"]
    }
  }
}

#多字段查询 之分词 可以对查询的关键字进行分词再查询
GET /ems/_search
{
  "query": {
    "query_string": {
      "query": "中国声音",
      "analyzer": "ik_max_word", 
      "fields": ["name","content"]
    }
  }
}

#分词分析 对于中文需要安装中文分词不然分的不好
GET /_analyze
{
  "text":"哈哈哈"
}

#过滤查询 对查的的数据再次过滤
GET /ems/_search
{
  "query": {
    "bool": {
      "must": [
        {
         "term": {
           "name": {
             "value": "小五"
           }
         }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 1,
            "lte": 10
          }
        }
      }
    }
  }
}


 

标签:search,12,name,GET,restAPI,练习,ems,query,es
来源: https://blog.csdn.net/qq_41082092/article/details/120324581

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有