ElasticSearch6.X查询及相关基本操作

文章目录

  • ElasticSearch6.X查询及相关基本操作
    • 基础概念
    • RESTFul API
    • 基本操作
      • 索引的创建
      • 插入
      • 修改
        • 直接修改
        • 脚本修改
      • 删除
        • 删除文档
        • 删除索引
      • 查询
        • 简单查询
        • 条件查询
          • 查询所有
          • 查询指定记录数
          • 关键字查询
          • 排序
        • 聚合查询 aggs
    • 高级查询
      • 子条件查询query和filter
        • Query context
          • 模糊匹配
          • 习语匹配查询 match_phrase
          • 多个字段的匹配查询 multi_match
          • 字段级别的查询 term
          • 字段级别的查询 range
        • Filter context
      • 复合查询
        • 常用查询

ElasticSearch6.X查询及相关基本操作

基础概念

  • 索引:含有相同属性的文档集合
       相当于与SQL中database
  • 类型:索引可以定义一个或多个类型,文档必须属于一个类型
       相当于与SQL中table
  • 文档:文档是可以被索引的基本数据单位
       相当于与SQL中row

假设一个系统中有多个汽车,图书,家具 等等索引
图书索引,按类型分为科普,小说,心理,历史等等类型。
具体到每一本书籍就是文档,最小的存储单位

  • 分片:每个索引都有多个分片,每个分片是一个Lucene索引

  • 备份:拷贝一份分片就完成了分片的备份

RESTFul API

  • api基本格式 http://[ip]:[port]/[索引]/[类型] /[文档id]
  • 常用HTTP动词 GET/PUT/DELETE/POST

基本操作

索引的创建

url :http://192.168.1.106:9200/people put

{
	"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"
				}
			}
		}
	}
}

创建索引类型
number_of_shards:指定分片数

number_of_replicas:指定副本数

插入

  • 指定文档id插入
    url:http://192.168.1.106:9200/people/man/1 put方式
    参入数据Json
{
	"name":"盖伦",
	"country":"American",
	"age":20,
	"date":"1998-01-01"
}

插入成功返回

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

此时用es head插件可以看出已经多出一条数据
ElasticSearch6.X查询及相关基本操作_第1张图片

  • 自动产生文档id插入
    url:http://192.168.1.106:9200/people/man post方式
{
	"name":"吕布",
	"country":"China",
	"age":20,
	"date":"1999-01-01"
}

插入成功返回

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

此时系统会给_id 生成一个id

修改

直接修改

url:http://192.168.1.106:9200/people/man/1/_update post方式
数据
ElasticSearch6.X查询及相关基本操作_第2张图片

{
	"doc":{
		"name":"赵信"
	}
}

将“盖伦” 修改名称为“赵信“,成功之后返回

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

ElasticSearch6.X查询及相关基本操作_第3张图片

脚本修改

还可以用脚本修改:如把年龄增加10岁。

{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age += 10"
	}
}
{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

ElasticSearch6.X查询及相关基本操作_第4张图片

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

成功返回数据

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

在这里插入图片描述

删除

删除文档

url:http://192.168.1.106:9200/people/man/1 delete方式

删除name为“赵信”的文档,成功返回

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

在这里插入图片描述

删除索引

url http://192.168.1.106:9200/people/ delete方式

查询

简单查询

首先初始化一些数据,创建索引book
put http://192.168.1.106:9200/book

{
	"settings":{
		"number_of_shards": 3,
		"number_of_replicas":1
	},
	"mappings":{
		"novel":{
			"properties":{
				"word_count":{
					"type":"integer"
				},
				"author":{
					"type":"keyword"
				},
				"title":{
					"type":"text"
				},
				"publish_date":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}
		}
	}
}

文档数据

{
	"author":"罗贯中",
	"title":"三国演义",
	"word_count":5000,
	"publish_date":"1995-01-10"
}

{
	"author":"陈寿",
	"title":"三国志",
	"word_count":10000,
	"publish_date":"2000-10-10"
}

{
	"author":"杰克",
	"title":"Java入门",
	"word_count":1000,
	"publish_date":"2010-01-10"
}

{
	"author":"bob",
	"title":"ElasticSearch菜谱",
	"word_count":2000,
	"publish_date":"2015-05-10"
}

{
	"author":"张三",
	"title":"ElasticSearch入门",
	"word_count":2000,
	"publish_date":"2015-05-10"
}

{
	"author":"张三",
	"title":"ElasticSearch精通",
	"word_count":2000,
	"publish_date":"2015-05-10"
}

{
	"author":"李四",
	"title":"ElasticSearch入门精通到放弃",
	"word_count":2000,
	"publish_date":"2015-05-10"
}

{
	"author":"王小波",
	"title":"黄金时代",
	"word_count":4000,
	"publish_date":"2000-07-10"
}

get http://192.168.1.106:9200/book/novel/1

{
    "_index": "book",
    "_type": "novel",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "author": "罗贯中",
        "title": "三国演义",
        "word_count": 5000,
        "publish_date": "1995-01-10"
    }
}

条件查询

查询所有

post http://192.168.1.106:9200/book/_search
参数:

{
	"query":{
		"match_all":{}
	}
}
查询指定记录数
{
	"query":{
		"match_all":{}
	},
	"from":1,
	"size":1
}
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 8,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "杰克",
                    "title": "Java入门",
                    "word_count": 1000,
                    "publish_date": "2010-01-10"
                }
            }
        ]
    }
}
关键字查询

post http://192.168.1.106:9200/book/_search

{
	"query":{
		"match":{
			"title":"三国"
		}
	}
}

显示带“三国”关键字的所有文档记录

{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 3.0884533,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 3.0884533,
                "_source": {
                    "author": "陈寿",
                    "title": "三国志",
                    "word_count": 10000,
                    "publish_date": "2000-10-10"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1.3097504,
                "_source": {
                    "author": "罗贯中",
                    "title": "三国演义",
                    "word_count": 5000,
                    "publish_date": "1995-01-10"
                }
            }
        ]
    }
}
排序
{
	"query":{
		"match":{
			"title":"三国"
		}
	},
	"sort":[
		{
			"publish_date":{
				"order":"asc"
			}
		}
	]
}

聚合查询 aggs

post http://192.168.1.106:9200/book/_search

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

返回结果:

 "aggregations": {
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 2000,
                    "doc_count": 4
                },
                {
                    "key": 1000,
                    "doc_count": 1
                },
                {
                    "key": 4000,
                    "doc_count": 1
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 10000,
                    "doc_count": 1
                }
            ]
        }
    }

多个聚合字段 terms
post http://192.168.1.106:9200/book/_search

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

字段统计 stats
post http://192.168.1.106:9200/book/_search

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

返回结果

 "aggregations": {
        "static_word_count": {
            "count": 8,
            "min": 1000,
            "max": 10000,
            "avg": 3500,
            "sum": 28000
        }
    }

还有min,max等等聚合查询

{
	"aggs":{
		"static_word_count":{
			"min":{
				"field":"word_count"
			}
		}
	}
}

高级查询

  • 子条件查询:特定字段查询所指特定值
  • 复合条件查询:以一定的逻辑组合子条件查询

子条件查询query和filter

Query context

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

  • 全文本查询:针对文本类型数据
  • 字段级别查询:针对结构化数据,如数字、日期等
模糊匹配
{
	"query":{
		"match":{
			"author":"张三"
		}
	}
}
习语匹配查询 match_phrase
{
	"query":{
		"match_phrase":{
			"title":"ElasticSearch入门"
		}
	}
}

结果

{
             "_index": "book",
              "_type": "novel",
              "_id": "5",
              "_score": 1.8012037,
              "_source": {
                  "author": "张三",
                  "title": "ElasticSearch入门",
                  "word_count": 2000,
                  "publish_date": "2015-05-10"
              }
          },
          {
              "_index": "book",
              "_type": "novel",
              "_id": "7",
              "_score": 1.147541,
              "_source": {
                  "author": "李四",
                  "title": "ElasticSearch入门精通到放弃",
                  "word_count": 2000,
                  "publish_date": "2015-05-10"
              }
          }
多个字段的匹配查询 multi_match

查询包含author和title都包含的“王小波”

{
	"query":{
		"multi_match":{
			"query":"王小波",
			"fields":["author","title"]
		}
	}
}

结果

            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 2.6694732,
                "_source": {
                    "author": "王小波",
                    "title": "王小波自传",
                    "word_count": 14000,
                    "publish_date": "2000-07-10"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 0.2876821,
                "_source": {
                    "author": "王小波",
                    "title": "黄金时代",
                    "word_count": 4000,
                    "publish_date": "2000-07-10"
                }
            }
字段级别的查询 term
{
	"query":{
		"term":{
			"author":"王五"
		}
	}
}

字段级别的查询 range

Integer类型查询

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

日期类型查询

{
	"query":{
		"range":{
			"publish_date":{
				"gt":"2015-01-01",
				"lte":"now"
			}
		}
	}
}

Filter context

在查询过程中,只判断改文档是否满足条件,只有Yes或者No

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

结果

 {
  "_index": "book",
         "_type": "novel",
         "_id": "4",
         "_score": 0,
         "_source": {
             "author": "杰克",
             "title": "Java入门",
             "word_count": 1000,
             "publish_date": "2010-01-10"
         }
     }

复合查询

常用查询

  • 固定分数查询

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

    boost:设定指定分数。

  • 布尔查询 或关系查询
    查询作者为“张三”或者标题是“ElasticSearch”的文档

    {
    	"query":{
    		"bool":{
    			"should":[
    				{
    					"match":{
    						"author":"张三"
    					}
    				},
    				{
    					"match":{
    						"title":"ElasticSearch"
    					}
    				}
    				
    			]
    		}
    	}
    }
    
    {
    	"query":{
    		"bool":{
    			"must":[
    				{
    					"match":{
    						"author":"张三"
    					}
    				},
    				{
    					"match":{
    						"title":"ElasticSearch"
    					}
    				}
    				
    			]
    		}
    	}
    }
    

你可能感兴趣的:(ElasticSearch)