首页 > 其他分享 >Elasticsearch 搜索 API

Elasticsearch 搜索 API

时间:2022-10-17 23:11:45浏览次数:75  
标签:city GET index twitter 索引 API 搜索 Elasticsearch type

Elasticsearch 搜索 API

目录

搜索多个索引

# 未指定文档时,返回的是所有索引的文档
# 默认返回 10 个文档
GET /_search
# 返回 20 个文档
GET /_search?size=20
# 对多个索引进行搜索
GET /twitter,test,catalog/_search
# 搜索以 index 开头的索引并且排除 index3
GET /index*,-index3/_search

搜索单个索引

# 搜索单个文档
# 默认返回 10 个文档
GET twitter/_search
# 分页查询
# from 从 0 开始
GET twitter/_search?size=2&from=2
# DFS 查询(Elasticsearch 的领域特定语言),分页查询
GET twitter/_search
{
  "size": 2,
  "from": 2,
  "query": {
    "match_all": {}
  }
}

只返回特定字段

# 只显示特定字段
# 只显示总数
GET twitter/_search?filter_path=hits.total
# 只返回评分和 city 字段
GET twitter/_search?filter_path=hits.hits._score,hits.hits._source.city
# 只返回 user 和 city
GET twitter/_search
{
  "_source": ["user", "city"],
  "query": {
    "match_all": {}
  }
}
# 只返回 user 和 city
# includes 和 excludes 参数支持通配符
GET twitter/_search
{
  "_source": {
    "includes": ["user", "city"]
  },
  "query": {
    "match_all": {}
  }
}
# 使用 fields 指定返回字段,不返回 _source
GET twitter/_search
{
  "_source": false,
  "fields": ["user", "city"],
  "query": {
    "match_all": {}
  }
}

统计文档数量

# 统计文档总数
GET twitter/_count
# 根据条件统计文档总数
GET twitter/_count
{
  "query": {
    "match": {
      "city": "北京"
    }
  }
}

查询索引配置

# 查询索引配置
GET twitter/_settings
# 配置索引,如果索引已经存在,需要删除后重新再索引
PUT twitter1
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

修改索引 Mapping

Elasticsearch 是 schemaless 的(无须预先创建索引和 Mapping 即可插入文档)

自动识别的字段类型可能不准确,如果知道要插入文档的字段类型,可以预先创建文档

# 查询索引 Mapping
GET twitter/_mapping

修正字段类型需要删除索引后在重新创建索引

# localtion 被自动识别成 text 类型,将其修正成 geo_point 类型
DELETE twitter

PUT twitter
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

PUT twitter/_mapping
{
  "properties": {
    "address": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "age": {
      "type": "long"
    },
    "city": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "country": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "location": {
      "type": "geo_point"
    },
    "message": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "province": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "uid": {
      "type": "long"
    },
    "user": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
GET twitter/_mapping
# 手动创建索引后,批量插入文档
POST _bulk
{"index":{"_index":"twitter","_id":1}}
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{"index":{"_index":"twitter","_id":2}}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{"index":{"_index":"twitter","_id":3}}
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{"index":{"_index":"twitter","_id":4}}
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{"index":{"_index":"twitter","_id":5}}
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{"index":{"_index":"twitter","_id":6}}
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}

参阅

标签:city,GET,index,twitter,索引,API,搜索,Elasticsearch,type
From: https://www.cnblogs.com/liaozibo/p/elasticsearch-search-api.html

相关文章

  • FedEx api获取token
     usingMicrosoft.AspNetCore.Mvc;usingRestSharp;usingSystem.Net;usingSystem.Text;usingNewtonsoft.Json;   publicstringFed_GetAccessToken()......
  • API网关
    一、网关相关配置1、在pom.xml引入依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2......
  • Java8中处理日期和时间的常用API
    场景java8中引入了一个新包java.time,包含了多数会用到的核心类。注:博客:https://blog.csdn.net/badao_liumang_qizhi关注公众号霸道的程序猿获取编程相关电子书、教......
  • 关于Azure-portal-虚拟机界面通过Private IP address-无法搜索到虚拟机的解决方法
    因Azure管理的机器越来越多了,今天需要去查看一台虚拟机的信息及做一些操作于是笔者登录到Azure-portal,进入到Virtualmachines界面,通过要处理的机器的内网私有IP地址,尽然......
  • #yyds干货盘点# LeetCode 热题 HOT 100:验证二叉搜索树
    题目:给你一个二叉树的根节点root,判断其是否是一个有效的二叉搜索树。有效二叉搜索树定义如下:节点的左子树只包含小于当前节点的数。节点的右子树只包含大于当前节点......
  • go-zero docker-compose 搭建课件服务(三):编写courseware api服务
    0、转载go-zerodocker-compose搭建课件服务(三):编写coursewareapi服务0.1源码地址https://github.com/liuyuede123/go-zero-courseware1、生成api相关文件#到之前创......
  • 全文搜索引擎Solr原理和实战教程
    Solr简介1.Solr是什么?Solr它是一种开放源码的、基于LuceneJava的搜索服务器,易于加入到Web应用程序中。Solr提供了层面搜索(就是统计)、命中醒目显示并且支持多......
  • Java基础(八)| 常用API与StringBuilder详解
    ⭐本专栏旨在对JAVA的基础语法及知识点进行全面且详细的讲解,完成从0到1的java学习,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握JAVA编程,同时为后续的框架学习,进阶开......
  • Vue3中 响应式 API ( readonly、shallowReadonly、toRaw、markRaw ) 详解
    1.readonly函数接受一个对象(不论是响应式还是普通的)或是一个ref,返回一个原值的只读代理。只读代理是深层的:对任何嵌套属性的访问都将是只读的。它的ref解包行为......
  • ElasticSearch【java提高】
    前言暑假持续学习ing​ElasticSearch官网地址​​https://www.elastic.co/cn/​​版本:ElasticSearch7.6.16.x7.x的区别十分大,6.x的API(原生API、RestFul高级)我们要讲解什么......