Elasticsearch 基本api

Elasticsearch 版本是 6.2.3

如何安装es

API

curl -X GET http://localhost:9200/_count

{
  "count": 0,
  "_shards": {
    "total": 0,
    "successful": 0,
    "skipped": 0,
    "failed": 0
  }
}
添加数据
curl -X PUT http://localhost:9200/megacorp/employee/1 -H 'Content-Type: application/json' -d'

{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}'
获得数据
curl -X GET http://localhost:9200/megacorp/employee/1?pretty

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 3,
  "found" : true,
  "_source" : {
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests" : [
      "sports",
      "music"
    ]
  }
}
查询
# 轻量搜索
curl -X GET http://localhost:9200/megacorp/employee/_search

# 轻量搜索
curl -X GET http://localhost:9200/megacorp/employee/_search?q=last_name:Smith

# 轻量搜索
curl -X GET http://localhost:9200/megacorp/employee/_search -H -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Fir"
        }
    }
}'

# 条件搜索
curl -X GET http://localhost:9200/megacorp/employee/_search -H 'Content-Type: application/json' -d'
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith"
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            }
        }
    }
}'

# 全文搜索
curl -XGET http://localhost:9200/megacorp/employee/_search -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}'

# 短语搜索
curl -X GET http://localhost:9200/megacorp/employee/_search -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}'

# 高亮搜索
curl -X GET http://localhost:9200/megacorp/employee/_search -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}'

分析聚合 aggregations

聚合搜索可能会报错

Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

应该是es5.x后对 排序/聚合 这些操作用单独的数据结构(fielddata)缓存到内存里了, 需要单独开启

curl -X PUT http://localhost:9200/megacorp/_mapping/employee/ -H 'Content-Type: application/json' -d'
{
  "properties": {
    "interests": {
      "type":     "text",
      "fielddata": true
    }
  }
}'
聚合查询
curl -XGET "http://localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}'

# 附带查询条件的聚合查询
curl -X GET http://localhost:9200/megacorp/employee/_search  -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}'

# 分级汇总, 比如 查询特定兴趣爱好员工的平均年龄:

curl -X GET http://localhost:9200/megacorp/employee/_search -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}'
通配符查询(模糊查询)
curl -X GET http://localhost:9200/eth/address/_search -H 'Content-Type: application/json' -d'
{
    "query" : {
        "wildcard" : {
            "address": "0x0000*"
        }
    }
}'
文档得分的计算过程
# 文档得分的计算过程
curl -X GET http://localhost:9200/megacorp/employee/1/_explain  -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "smith"
        }
    }
}'

参考:

https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/bulk.html
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/search-lite.html
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/running-elasticsearch.html#running-elasticsearch
https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Elasticsearch是一个开源的、高度可扩展的全文搜索与分析引擎。它可以存储海量的数据,能近乎实时地搜索和分...
    VastVanity阅读 3,222评论 0 0
  • ElasticSearch是一个高度可扩展的开源搜索引擎并使用REST API,所以您值得拥有。 在本教程中,将介...
    易百教程阅读 5,871评论 0 15
  • 基础概念 Elasticsearch有几个核心概念,从一开始理解这些概念会对整个学习过程有莫大的帮助。 接近实时(...
    山天大畜阅读 6,321评论 0 4
  • 晨起,游走于小区花园之间,弊了一晚上的狗狗撒着欢的奔跑,恣意洒脱。三三两两早锻炼的人或走或跑沿着小区外围挥洒着属于...
    墨莫0801阅读 1,720评论 0 0
  • 过于通透的人生是痛苦的,永恒的痛苦,没一点商量的余地。所以人无须要太过明白,明白就好。
    北海有玄鸟阅读 708评论 0 0

友情链接更多精彩内容