欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ES基础知识-笔记

程序员文章站 2022-05-01 17:13:31
索引 索引是由英文小写字母组成,且不含中划线。...

elasticsearch-head启动:

npm run start

基本概念

索引
含有相同属性的文档的集合。
索引是由英文小写字母组成,且不含中划线。
类型
索引可以定义一个或多个类型,文档必须属于一个类型。
文档
文档是可以被索引的基本数据单位。
分片
每个索引都有多个分片,每个分片都是一个lucene索引。
备份
拷贝一份分片就完成了分片的备份。

基本用法

api的基本格式

http://<ip>:<port>/<索引>/<类型>/<文档id>

使用put方法创建索引:

PUT http://localhost:9200/people

创建索引的参数:

{
	"settings":{
		"number_of_shards":3,
		"number_of_replicas":1
	},
	"mappings":{
		"man":{
			"properties":{
				"name":{
					"type":"text"
				},
				"country":{
					"type":"keyword"
				},
				"age":{
					"type":"integer"
				},
				"date":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
				}
			}
		}
	}
}

注意的是elasticsearch6只支持一个索引创建一种类型,不支持一个索引创建多种类型。

插入
指定文档id插入:

PUT  http://localhost:9200/people/man/1
{
	"name":"Robb",
	"country":"wolf",
	"age":19,
	"date":"1987-03-29"
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

不指定文档id插入:

POST  http://localhost:9200/people/man/
{
	"name":"Talisha",
	"country":"free",
	"age":18,
	"date":"1989-03-29"
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "hDT0q2YB6miiGVIFvpyC",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

修改
直接修改,指定id

POST http://localhost:9200/people/man/1/_update
{
	"doc":{
		"name":"fire"
	}
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 3
}

脚本修改:
年龄+10

POST http://localhost:9200/people/man/1/_update
{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age += 10"
	}
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 3
}

在修改部分还可以使用外部参数:

{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age = params.age",
		"params":{
			"age":19
		}
	}
}

删除

删除索引people中类型为man中id为1的文档。

DELETE http://localhost:9200/people/man/1

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 5,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 4
}

删除索引book

DELETE http://localhost:9200/book

返回结果:

{
    "acknowledged": true
}

查询

简单查询
新建索引:

PUT http://localhost:9200/novel
{
	"mappings":{
		"swords":{
			"properties":{
				"title":{
					"type":"text"
				},
				"author":{
					"type":"keyword"
				},
				"word_count":{
					"type":"integer"	
				},
				"publish_date":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}	
		}	
	}
}

插入数据:

PUT http://localhost:9200/novel/swords/11
{
	"title":"七十二变",
	"author":"孙悟空",
	"word_count":1000,
	"publish_date":"2000-10-01"
}

插入结果:
ES基础知识-笔记查询全部

POST http://localhost:9200/novel/_search
{
	"query":{
		"match_all":{
			
		}	
	}
}

分页:

{
	"query":{
		"match_all":{
			
		}	
	},
	"from":1,
	"size":2
}

条件查询
根据条件查询:

{
	"query":{
		"match":{
			"title":"ElasticSearch"
		}	
	}
}

返回结果:

{
    "took": 89,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.86312973,
        "hits": [
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "4",
                "_score": 0.86312973,
                "_source": {
                    "title": "ElasticSearch大法好",
                    "author": "李四",
                    "word_count": 1000,
                    "publish_date": "2017-08-01"
                }
            },
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "8",
                "_score": 0.6682933,
                "_source": {
                    "title": "ElasticSearch入门",
                    "author": "瓦力",
                    "word_count": 3000,
                    "publish_date": "2017-08-20"
                }
            },
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "9",
                "_score": 0.6682933,
                "_source": {
                    "title": "ElasticSearch精通",
                    "author": "很胖的瓦力",
                    "word_count": 3000,
                    "publish_date": "2017-08-15"
                }
            }
        ]
    }
}

带排序的条件查询:

{
	"query":{
		"match":{
			"title":"ElasticSearch"
		}	
	},
	"sort":{
		"publish_date":{"order":"desc"}	
	}
}

返回结果:

{
    "took": 414,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "8",
                "_score": null,
                "_source": {
                    "title": "ElasticSearch入门",
                    "author": "瓦力",
                    "word_count": 3000,
                    "publish_date": "2017-08-20"
                },
                "sort": [
                    1503187200000
                ]
            },
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "9",
                "_score": null,
                "_source": {
                    "title": "ElasticSearch精通",
                    "author": "很胖的瓦力",
                    "word_count": 3000,
                    "publish_date": "2017-08-15"
                },
                "sort": [
                    1502755200000
                ]
            },
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "4",
                "_score": null,
                "_source": {
                    "title": "ElasticSearch大法好",
                    "author": "李四",
                    "word_count": 1000,
                    "publish_date": "2017-08-01"
                },
                "sort": [
                    1501545600000
                ]
            }
        ]
    }
}

聚合查询
单条件聚合:

{
    "aggs": {
        "group_by_word_count": {
            "terms": {
            	"field":"word_count"
            }
        }
    }
}

返回结果:

{
    "took": 201,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 1,
        "hits": [
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "title": "菜谱",
                    "author": "王五",
                    "word_count": 5000,
                    "publish_date": "2002-10-01"
                }
            },
           。。。
           ,
            {
                "_index": "novel",
                "_type": "swords",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "title": "Python入门",
                    "author": "张四",
                    "word_count": 2000,
                    "publish_date": "2005-10-01"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1000,
                    "doc_count": 5
                },
                {
                    "key": 2000,
                    "doc_count": 2
                },
                {
                    "key": 3000,
                    "doc_count": 2
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 10000,
                    "doc_count": 1
                }
            ]
        }
    }
}

多条件聚合:

{
    "aggs": {
        "group_by_word_count": {
            "terms": {
            	"field":"word_count"
            }
        },
        "group_by_publish_date": {
            "terms": {
            	"field":"publish_date"
            }
        }
    }
}

关键结果信息:

 "aggregations": {
        "group_by_publish_date": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 970358400000,
                    "key_as_string": "2000-10-01 00:00:00",
                    "doc_count": 4
                },
                {
                    "key": 852076800000,
                    "key_as_string": "1997-01-01 00:00:00",
                    "doc_count": 2
                },
                {
                    "key": 1033430400000,
                    "key_as_string": "2002-10-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1128124800000,
                    "key_as_string": "2005-10-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1501545600000,
                    "key_as_string": "2017-08-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1502755200000,
                    "key_as_string": "2017-08-15 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1503187200000,
                    "key_as_string": "2017-08-20 00:00:00",
                    "doc_count": 1
                }
            ]
        },
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1000,
                    "doc_count": 5
                },
                {
                    "key": 2000,
                    "doc_count": 2
                },
                {
                    "key": 3000,
                    "doc_count": 2
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 10000,
                    "doc_count": 1
                }
            ]
        }
    }

功能函数:

{
    "aggs": {
        "grades_word_count": {
            "stats": {
            	"field":"word_count"
            }
        }
    }
}

关键返回信息:

 "aggregations": {
        "grades_word_count": {
            "count": 11,
            "min": 1000,
            "max": 10000,
            "avg": 2727.2727272727275,
            "sum": 30000
        }
    }

高级查询

query
子条件查询

特定字段查询所指特定值
Query context
在查询的过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的有多好。

全文本查询
针对文本类型的数据
普通查询:模糊查询,会分词查询

POST http://localhost:9200/novel/_search
{
	"query":{
		"match":{
			"title":"ElasticSearch入门"
		}	
	}
}

结果:
ES基础知识-笔记使用习语查询可解决这个问题:

{
	"query":{
		"match_phrase":{
			"title":"ElasticSearch入门"
		}	
	}
}

结果:
ES基础知识-笔记一个值在多个字段中存在的查询:

{
	"query":{
		"multi_match":{
			"query":"瓦力",
			"fields":["author","title"]
		}	
	}
}

语法查询:存在语法逻辑

{
	"query":{
		"query_string":{
			"query":"(ElasticSearch and 大法) or Python"
		}	
	}
}
{
	"query":{
		"query_string":{
			"query":"瓦力 or ElasticSearch",
			"fields":["author", "title"]
		}	
	}
}

字段级别的查询
针对结构化得数据,如数字、日期等。
字段查询:

{
	"query":{
		"term":{
			"author":"瓦力"
		}	
	}
}

范围查询:

{
	"query":{
		"range":{
			"word_count":{
				"gt":1000,
				"lte":2000
			}
		}	
	}
}

时间范围查询:

{
	"query":{
		"range":{
			"publish_date":{
				"gt":"2017-01-01",
				"lte":"2017-12-31"
			}
		}	
	}
}

子条件查询
Filter context
在查询过程中,只判断该文档是否满足条件,只有YES或NO。

{
	"query":{
		"bool":{
			"filter":{
				"term":{
					"word_count":1000
				}	
			}
		}
	}
}

filter需要结合bool使用。

复合条件查询

以一定的逻辑组合子查询
固定查询分数

{
	"query":{
		"constant_score":{
			"filter":{
				"match":{
					"title":"ElasticSearch"
				}
			}
		}
	}
}
{
	"query":{
		"constant_score":{
			"filter":{
				"match":{
					"title":"ElasticSearch"
				}
			},
			"boost":2
		}
	}
}

多个条件,或(should):

{
	"query":{
		"bool":{
			"should":[
				{
					"match":{
						"author":"瓦力"
					}
				},
				{
					"match":{
						"title":"ElasticSearch"
					}
				}
				]
		}
	}
}

多个条件,与(must):

{
	"query":{
		"bool":{
			"must":[
				{
					"match":{
						"author":"瓦力"
					}
				},
				{
					"match":{
						"title":"ElasticSearch"
					}
				}
				]
		}
	}
}

先条件,后过滤:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                }
            ],
            "filter": [
                {
                    "match": {
                        "word_count": 3000
                    }
                }
            ]
        }
    }
}

排除:

{
    "query": {
        "bool": {
           "must_not":{
           		"term":{
           			"author":"瓦力"
           		}
           }
        }
    }
}

本文地址:https://blog.csdn.net/YaxiongJiang/article/details/82729839