ElasticSearch基于RestFul的常用操作

版权声明:本文为博主(yjclsx)的原创文章,未经博主允许不得转载。 https://blog.csdn.net/yjclsx/article/details/86537095

1、创建index

PUT http://127.0.0.1:9200/my_es2
{
	"mappings":{
		"logs":{
			"properties":{
				"name":{
					"type":"text"
				},
				"age":{
					"type":"integer"
				},
				"about":{
					"type":"text"
				},
				"interests":{
					"type":"text"
				},
				"birthDay":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss"
				}
			}
		}
	}
}

创建index(数据库):my_es2,type(表):logs,field(字段):name(姓名,文本类型)、age(年龄,数字类型)、about(简介,文本类型)、interests(兴趣,文本类型)、birthDay(生日,时间类型,格式是yyyy-MM-dd HH:mm:ss)。

2、插入数据

插入3条数据到logs中

POST http://127.0.0.1:9200/my_es2/logs/1
{
	"name": "yjc",
	"age": 27,
	"about": "是的,I love lsx",
	"interests": "啦啦啦",
	"birthDay": "2017-01-01 18:36:23"
}

POST http://127.0.0.1:9200/my_es2/logs/2
{
	"name": "lsx",
	"age": 26,
	"about": "没错,I love yjc",
	"interests": "哈哈哈",
	"birthDay": "2018-02-02 19:37:24"
}

POST http://127.0.0.1:9200/my_es2/logs/3
{
	"name": "yjclsx",
	"age": 0,
	"about": "萌萌哒,I love yjc and lsx",
	"interests": "嘻嘻嘻",
	"birthDay": "2018-03-03 20:38:25"
}

3、查询

查询生日在2017-01-01到2019-01-01之间、姓名/简介/兴趣中带有关键字"yjc"的人员信息,并按照生日倒叙排序

注意(multi_match查询是种分词查询,带有关键字"yjc"是指分词后带有"yjc",所以"I love yjc !"可以匹配到,而"Iloveyjc!"是匹配不到的)

POST http://127.0.0.1:9200/my_es2/logs/_search
{
    "query": {
    	"bool":{
    		"must":{
    			"multi_match": {
            		"query": "yjc",
            		"fields": [
            			"name",
                		"about",
                		"interests"
            		],
            		"type": "phrase"
        		}
    		},
    		"filter":{
    			"range" : {
		            "birthDay": {
		                "gte": "2017-01-01 00:00:00",
		                "lte": "2019-01-01 00:00:00"
		            }
		        }
    		}
    	}
    },
    "sort": [
        { "birthDay": {"order":"desc"}}
    ]
}

返回结果如下

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "3",
                "_score": null,
                "_source": {
                    "name": "yjclsx",
                    "age": 0,
                    "about": "萌萌哒,I love yjc and lsx",
                    "interests": "嘻嘻嘻",
                    "birthDay": "2018-03-03 20:38:25"
                },
                "sort": [
                    1520109505000
                ]
            },
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "2",
                "_score": null,
                "_source": {
                    "name": "lsx",
                    "age": 26,
                    "about": "没错,I love yjc",
                    "interests": "哈哈哈",
                    "birthDay": "2018-02-02 19:37:24"
                },
                "sort": [
                    1517600244000
                ]
            },
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "1",
                "_score": null,
                "_source": {
                    "name": "yjc",
                    "age": 27,
                    "about": "是的,I love lsx",
                    "interests": "啦啦啦",
                    "birthDay": "2017-01-01 18:36:23"
                },
                "sort": [
                    1483295783000
                ]
            }
        ]
    }
}

4、增加字段

在logs中增加一个文本类型的字段address,用于表示住址

PUT http://127.0.0.1:9200/my_es2/_mapping/logs
{
	"properties":{
		"address":{
			"type":"text"
		}
	}
}

为address字段赋初值 

POST http://127.0.0.1:9200/my_es2/_update_by_query
{
  "script": {
    "lang": "painless",
    "inline": "if (ctx._source.address == null) {ctx._source.address = ''}"
  }
}

 再次执行步骤3中的查询语句,返回结果如下

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "3",
                "_score": null,
                "_source": {
                    "birthDay": "2018-03-03 20:38:25",
                    "address": "",
                    "name": "yjclsx",
                    "about": "萌萌哒,I love yjc and lsx",
                    "interests": "嘻嘻嘻",
                    "age": 0
                },
                "sort": [
                    1520109505000
                ]
            },
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "2",
                "_score": null,
                "_source": {
                    "birthDay": "2018-02-02 19:37:24",
                    "address": "",
                    "name": "lsx",
                    "about": "没错,I love yjc",
                    "interests": "哈哈哈",
                    "age": 26
                },
                "sort": [
                    1517600244000
                ]
            },
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "1",
                "_score": null,
                "_source": {
                    "birthDay": "2017-01-01 18:36:23",
                    "address": "",
                    "name": "yjc",
                    "about": "是的,I love lsx",
                    "interests": "啦啦啦",
                    "age": 27
                },
                "sort": [
                    1483295783000
                ]
            }
        ]
    }
}

可见,里面已经增加了address字段。 

5、更新数据 

修改id为3的人员信息

POST http://127.0.0.1:9200/my_es2/logs/3/_update
{
    "doc": {
    	"address":"住址1",
    	"age":1
    }
}

 再查询一下id为3的人员信息

GET http://127.0.0.1:9200/my_es2/logs/_search?q=_id:3

返回结果如下

{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "my_es2",
                "_type": "logs",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "birthDay": "2018-03-03 20:38:25",
                    "address": "住址1",
                    "name": "yjclsx",
                    "about": "萌萌哒,I love yjc and lsx",
                    "interests": "嘻嘻嘻",
                    "age": 1
                }
            }
        ]
    }
}

 可见,已经修改成功了。

附:Elasticsearch 常用基本查询

猜你喜欢

转载自blog.csdn.net/yjclsx/article/details/86537095
今日推荐