elasticsearch索引的创建以及api的使用

本文介绍了如何在Kibana中创建Elasticsearch索引,定义mapping以设置字段类型,如将name设为文本类型,age设为整数,createTime设为长整型。之后展示了插入文档、查询文档的过程,以及如何通过JavaAPI进行查找操作。
摘要由CSDN通过智能技术生成

1. 使用kibana创建索引

之前看到过很多次mapping关键词,但是一直都没有太在意过,看了一些文档,mapping主要是为了定义索引中的一些属性,比如一个字段是什么类型的,以及一个字段是否能被索引等等
官网介绍:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_creating_an_index.html

# Click the Variables button, above, to create your own variables.
PUT /cyx_first_index
{
  "settings": { #设置相关
    "number_of_shards": 3, #分片数量
    "number_of_replicas": 2, #副本数量
    "analysis": { #解析器
      "analyzer": {
        "es_std": {
          "type": "standard" #标准版
        }
      }
    }
  },
  "mappings": {
    "properties": { #摸索了好久,最后还是问了chatgpt才搞定,chatgpt yyds
      "name":{"type": "text"}, # 7.0后string被废弃了,使用text 或者 keyword(不可分词)
      "age":{"type": "integer"},
      "createTime":{"type": "long"}
    }
  }
}

获取mapping
语法是get /index/_mapping

GET /cyx_first_index/_mapping

结果

{
  "cyx_first_index": {
    "mappings": {
      "properties": {
        "age": {
          "type": "integer"
        },
        "createTime": {
          "type": "long"
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}

2. 插入文档

POST /cyx_first_index/_doc
{
  "name":"chen",
  "age":27,
  "createTime":801848820000
}

查找

# Click the Variables button, above, to create your own variables.
POST /cyx_first_index/_search
{
  "query": {
    "match": {
      "name": "chen"
    }
  }
}

response

{
  "took": 538,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "cyx_first_index",
        "_id": "zcm_iocBg5Ow_pYU0yPl",
        "_score": 0.2876821,
        "_source": {
          "name": "chen",
          "age": 27,
          "createTime": 801848820000
        }
      }
    ]
  }
}

2.1 使用java api查找

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值