elasticsearch基础使用

(1).在elasticsearch-head插件手工创建索引,索引名称learn,索引相当于数据库

(2).创建类型,并设置类型的mapping,相当于创建表,并设置表结构

类型为video相当于表,mapping相当于为表设置结构

请求地址:http://localhost:9200/learn/

请求方式:PUT

请求内容:

{
  "mappings": {
    "video": {
      "properties": {
        "name": {
          "type": "text"
        },
        "cat_id": {
          "type": "integer"
        },
        "image": {
          "type": "text"
        },
        "url": {
          "type": "text"
        },
        "type": {
          "type": "byte"
        },
        "content": {
          "type": "text"
        },
        "uploader": {
          "type": "keyword"
        },
        "create_time": {
          "type": "integer"
        },
        "update_time": {
          "type": "integer"
        },
        "status": {
          "type": "byte"
        },
        "video_id": {
          "type": "keyword"
        }
      }
    }
  }
}

其实我们不设置mapping,es也会自动检测文档类型设置maping

(3).索引一个文档记录,手动指定记录id,相当于新增记录

请求地址:http://localhost:9200/learn/video/1/

请求方式:PUT

请求内容:

{
"name":"林芳臻",
"cat_id":1,
"image":"http://www.gaojiufeng.cn/a.png",
"url":"http://www.gaojiufeng.cn/a.html",
"type":1,
"uploader":"gao",
"status":1,
"video_id":"linfangzhen"
}

记录id为1,如果我们是想把mysql的数据同步到es中,那么我们自己传递id很有必要

(4).索引一个文档记录,使用es自动生成id,相当于新增记录

请求地址:http://localhost:9200/learn/video/

请求方式:POST

请求内容:

{
"name":"林芳臻3",
"cat_id":1,
"image":"http://www.gaojiufeng.cn/a.png",
"url":"http://www.gaojiufeng.cn/a.html",
"type":1,
"uploader":"gao",
"status":1,
"video_id":"linfangzhen"
}

记录id为0eeyRnIBO2vtaZYtd_La

(5).查询一个文档记录,关键字分词查询

请求地址:http://localhost:9200/learn/video/

请求操作:_search

请求方式:POST

请求内容:

{
    "query":{
        "match":{
            "name":"林芳臻"
        }
    }
}

返回结果:

{
    "took":12,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":3,
        "max_score":0.5809142,
        "hits":[
            {
                "_index":"learn",
                "_type":"video",
                "_id":"2",
                "_score":0.5809142,
                "_source":{
                    "name":"林芳臻",
                    "cat_id":1,
                    "image":"http://www.gaojiufeng.cn/a.png",
                    "url":"http://www.gaojiufeng.cn/a.html",
                    "type":1,
                    "uploader":"gao",
                    "status":1,
                    "video_id":"linfangzhen"
                }
            },
            {
                "_index":"learn",
                "_type":"video",
                "_id":"0eeyRnIBO2vtaZYtd_La",
                "_score":0.51676416,
                "_source":{
                    "name":"林芳臻3",
                    "cat_id":1,
                    "image":"http://www.gaojiufeng.cn/a.png",
                    "url":"http://www.gaojiufeng.cn/a.html",
                    "type":1,
                    "uploader":"gao",
                    "status":1,
                    "video_id":"linfangzhen"
                }
            },
            {
                "_index":"learn",
                "_type":"video",
                "_id":"1",
                "_score":0.2876821,
                "_source":{
                    "name":"臻子",
                    "cat_id":1,
                    "image":"http://www.gaojiufeng.cn/a.png",
                    "url":"http://www.gaojiufeng.cn/a.html",
                    "type":1,
                    "uploader":"gao",
                    "status":1,
                    "video_id":"linfangzhen"
                }
            }
        ]
    }
}

搜索的结果中不仅仅包含林芳臻,还包含臻子,说明es中使用match搜索会对关键词进行分词查询,林芳臻被分为“林”,“芳”,“臻”3个字来查询,但是可以看到每个结果得到的匹配分数是不同的。但是有的时候我们只是想搜索包含林芳臻的结果,该怎么搜索,请看下面。

(6).查询一个文档记录,关键字不分词查询

请求地址:http://localhost:9200/learn/video/

请求操作:_search

请求方式:POST

请求内容:

{
    "query":{
        "match_phrase":{
            "name":"林芳臻"
        }
    }
}

返回结果:

{"took": 128,
"timed_out": false,
"_shards": {},
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
"hits": {}
{}
,
{}
"name": "林芳臻",
"cat_id": 1,
"image": "http://www.gaojiufeng.cn/a.png",
"url": "http://www.gaojiufeng.cn/a.html",
"type": 1,
"uploader": "gao",
"status": 1,
"video_id": "linfangzhen"
"_index": "learn",
"_type": "video",
"_id": "2",
"_score": 0.5809142,
"_source": {}
"name": "林芳臻3",
"cat_id": 1,
"image": "http://www.gaojiufeng.cn/a.png",
"url": "http://www.gaojiufeng.cn/a.html",
"type": 1,
"uploader": "gao",
"status": 1,
"video_id": "linfangzhen"
"_index": "learn",
"_type": "video",
"_id": "0eeyRnIBO2vtaZYtd_La",
"_score": 0.5167642,
"_source": {}
"total": 2,
"max_score": 0.5809142,
"hits": []
}

使用match_phrase匹配到的结果只会包含关键字

(7).查询一个文档,自定义聚合数据,类似于group,比如统计每个分类cat_id下有多少条数据

请求地址:http://localhost:9200/learn/video/

请求操作:_search

请求方式:POST

请求内容:

{
    "aggs":{
        "id_result":{
            "terms":{
                "field":"cat_id"
            }
        }
    }
}

返回结果:

{
    "aggregations":{
        "id_result":{
            "doc_count_error_upper_bound":0,
            "sum_other_doc_count":0,
            "buckets":[
                {
                    "key":1,
                    "doc_count":3
                },
                {
                    "key":2,
                    "doc_count":1
                }
            ]
        }
    }
}

统计出cat_id分类有2个分类,分别为catid1和2,并且统计文档数量

访客
邮箱
网址

通用的占位符缩略图

人工智能机器人,扫码免费帮你完成工作


  • 自动写文案
  • 自动写小说
  • 马上扫码让Ai帮你完成工作
通用的占位符缩略图

人工智能机器人,扫码免费帮你完成工作

  • 自动写论文
  • 自动写软件
  • 我不是人,但是我比人更聪明,我是强大的Ai
Top