elasticsearch之索引

使用elasticsearch7.6.0版本。

安装

最新版本下载地扯
历史版本下载地扯
python之elasticsearch版本库
elasticsearch之python官方手册

介绍

索引就相当于数据库中的一张表,索引中的文档就相当于表中的信息。

创建索引

指定 mapping 和 settings

put http://localhost:9200/movice -d 
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      }
    }
  },
  "settings" : {
    "index" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 2
    }
  }
}

解释:
properties里的name就是相当于表中的字段信息。

数据类型

参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.6/mapping-types.html

如下图:

查看所有索引

http://localhost:9200/_cat/indices?v

如下图:

查看安装的插件

get localhost:9200/_cat/plugins

查看索引信息

get http://localhost:9200/movice

如下图:

创建文档时自动创建索引

比如在我的elasticsearch中。 我的索引student是不存在的。

指定id

post http://localhost:9200/student/_doc/8

如下图:

响应结果说明:

  • _index:文档所在的索引名
  • _type:文档所在的类型名
  • _id:文档ID
  • _version:文档的版本
  • result:created已经创建
  • _shards: _shards表示索引操作的复制过程的信息。
  • total:指示应在其上执行索引操作的分片副本(主分片和副本分片)的数量。
  • successful:表示索引操作成功的分片副本数。
  • failed:在副本分片上索引操作失败的情况下包含复制相关错误。

指定id的话就会使用指定的id,如果不指定id的话就会使用随机字符串

不指定id

post http://localhost:9200/student/_doc

如下图:

查看索引中的所有文档信息

get http://localhost:9200/student/_search

如下图:

模糊查询文档

get http://localhost:9200/student/_search -d 
{
	"query":{
		"match":{
			"name":"我"
		}
	}
}

如下图:

返回值:

{
	"took": 53,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 0.8712301,
		"hits": [
			{
				"_index": "student",
				"_type": "_doc",
				"_id": "7",
				"_score": 0.8712301,
				"_source": {
					"name": "我来了"
				}
			},
			{
				"_index": "student",
				"_type": "_doc",
				"_id": "8",
				"_score": 0.77391195,
				"_source": {
					"name": "我是8号"
				}
			},
			{
				"_index": "student",
				"_type": "_doc",
				"_id": "neypDHIBujABphC4Wrfw",
				"_score": 0.49657142,
				"_source": {
					"name": "我是九号,没有指定id"
				}
			}
		]
	}
}

分页查询

get http://localhost:9200/student/_search -d
{
	"query":{
		"match_all":{}
	},
	"from":0,
	"size":2
}

解释:from:从第1页开始显示,只显示2个文档信息。

如下图:

返回值:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 7,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "student",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "name": "张三"
        }
      },
      {
        "_index": "student",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "name": "李四"
        }
      }
    ]
  }
}

指定查询出来的文档信息

get http://localhost:9200/student/_search
{
  "query": { "match_all": {} },
  "_source": ["name"]
}

如下图:

其他博主文档

类型映射
elasticsearch文档

原文地址:https://www.cnblogs.com/zhenzi0322/p/12882003.html