C/C++教程

ElasticSearch常用API

本文主要是介绍ElasticSearch常用API,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

  • 查看节点信息
  • 索引操作
  • 映射操作
  • 文档操作
  • 复杂查询操作
  • 分词器操作

查看节点信息

GET _cat/count
GET _cat/health
GET _cat/indices
GET _cat/master
GET _cat/nodes

索引操作

创建索引

PUT user

查询索引

GET user

删除索引

DELETE user

映射操作

创建mapping

PUT my_index
{
  "mappings": {
    "properties": {
      "age": {"type": "integer"},
      "email": {"type": "keyword"},
      "name": {"type": "text"}
    }
  }
}

给mapping添加字段

PUT my_index/_mapping
{
  "properties":{
    "employee-id":{
      "type": "long",
      "index": false
    }
  }
}

修改索引字段是不可以的,会影响历史数据,只能通过数据迁移的方式
旧版本的索引有type,所以迁移时需要加上type

PUT newbank
{
  "mappings": {
    "properties" : {
        "account_number" : {
          "type" : "long"
        },
        "address" : {
          "type" : "text"
        },
        "age" : {
          "type" : "long"
        },
        "balance" : {
          "type" : "long"
        },
        "city" : {
          "type" : "text"
        },
        "email" : {
          "type" : "text"
        },
        "employer" : {
          "type" : "text"
        },
        "firstname" : {
          "type" : "text"
        },
        "gender" : {
          "type" : "text"
        },
        "lastname" : {
          "type" : "text"
        },
        "state" : {
          "type" : "text"
        }
      }
  }
}

POST _reindex
{
  "source": {
    "index": "bank"
  }, "dest": {
    "index": "newbank"
  }
}

查询mapping

GET bank/_mapping
GET newbank/_mapping
GET newbank/_doc/1

文档操作

插入和修改

# 幂等性,必须带id,插入或修改
PUT user/_doc/1007
{
  "name": "zhang2",
  "age":18,
  "sex":"nan"
}
#指明创建 可用put或post
PUT user/_doc/1009/_create  
{
  "name":"zhaoliu",
    "age":20
}
# 可以不带id,不带id时,id自动生成,相当于create;
# 带id时,首次为create,其余为update
# 修改会覆盖原值
POST user/_doc/1006
{
  "name": "lisi12223",
  "sex":"nan"
}
# 指明更新  不会覆盖原值
POST user/_doc/1007/_update
{
  "doc":{
    "name": "wangwu"
  }
}

乐观锁

# version在新版本中已经不可用,
# 替代为使用if_seq_no和if_primary_term
POST user/_doc/1?version=4
{
  "name": "lisi12223"
}
#新版本使用if_seq_no和if_primary_term实现乐观锁
POST user/_doc/1?if_seq_no=4&if_primary_term=1
{
  "name": "lisi12223"
}

删除

DELETE user/_doc/1

查询

# 查询指定id的文档
GET user/_doc/1007

批量操作
#批量添加数据 测试数据见
POST /bank/_doc/_bulk
{
ES官方测试数据
}

复杂查询操作

Query DSL语法

#全文搜索,相当于select *
GET _search
{
  "query": {
    "match_all": {}
  }
}

排序

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "account_number": {
        "order": "asc"
      },
      "balance": "desc"
    }
  ]
}

返回指定字段

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "_source": [
    "balance",
    "account_number",
    "firstname",
    "lastname",
    "age",
    "address"
  ]
}
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "_source": false,
  "fields": [
    "balance",
    "account_number",
    "firstname",
    "lastname",
    "age",
    "address"
  ]
}

分页

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}

match匹配 必须加字段条件,满足条件分更好

#条件为非字符时精确查询
GET bank/_search
{
  "query": {
    "match": {
      "account_number": "1"
    }
  } 
}
# 条件为字符 倒排索引 全文检索 会对条件进行分词 
# 最终按_score由高到低排序
GET bank/_search
{
  "query": {
    "match": {
      "address": "kings lane"
    }
  }
}
# 短语匹配 当成一个词,匹配包含这个词的字段值
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "kings lane"
    }
  }
}
# 前缀
GET bank/_search
{
  "query": {
    "match_phrase_prefix": {
      "address": "880"
    }
  }
}
# 多字段匹配
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "Holmes lane",
      "fields": ["address", "email"]
    }
  }
}

布尔 多条件查询

# 满足should的条件,评分更高
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "gender": "M"
          }
        },
        {
          "match": {
            "address": "kings"
          }
        }
      ],
      "should": [
        {
          "match": {
            "firstname": "Elinor"
          }
        }
      ]
    }
  }
}

范围查询

GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}

filter(must_not) 不会参与计算相关性得分,只过滤条件,速度相比match更快

GET bank/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}

term用于非text类型,例如数值,全文检索用match

# term 存在数据分析问题,也就是分词,检索text字段的完整值是困难的
GET bank/_search
{
  "query": {
    "term": {
      "address": {
        "value": "mill"
      }
    }
  }
}
#精确查询 address.keyword
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "990 Mill Road"
    }
  }
}

聚合查询

# 查询address为mill的用户及其年龄的分布和薪资平均值
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "aggAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "aggAvg": {
      "avg": {
        "field": "balance"
      }
    }
  }
}
# 按照年龄聚合,并请求这些年龄段的这些人的平均薪资
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0, 
  "aggs": {
    "aggAgg": {
      "terms": {
        "field": "age",
        "size": 1000
      },
      "aggs": {
        "aggAvg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}
# 查询所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资,以及这个年龄段的总体平均薪资和所有年龄段的总体平均薪资
# aggs套娃,基于上一次计算结果
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "aggAgg": {
      "terms": {
        "field": "age",
        "size": 10
      },
      "aggs": {
        "aggGender": {
          "terms": {
            "field": "gender.keyword",
            "size": 10
          },
          "aggs": {
            "aggBalance2": {
            "avg": {
              "field": "balance"
            }
          }
          }
        },
        "aggBalance1": {
          "avg": {
            "field": "balance"
          }
        }
      }
    } ,
    "aggBalance": {
      "avg": {
        "field": "balance"
      }
    }
  }
}

分词器操作

# 标准分词器
POST _analyze
{
  "analyzer": "standard",
  "text": ["wo ai zhong guo", "我爱中国"]
}
# ik分词器(包含中文分词)
POST _analyze
{
  "analyzer": "ik_smart",
  "text": ["wo ai zhong guo", "我爱中国"]
}
POST _analyze
{
  "analyzer": "ik_max_word",
  "text": ["wo ai zhong guo", "我爱中国"]
}
这篇关于ElasticSearch常用API的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!