一、雇员表查询
1.添加以下三条信息到Elasticsearch,index为megacorp,type为employee,id分别为1,2,3
{
“first_name” : “John”,
“last_name” : “Smith”,
“age” : 25,
“about” : “I love to go rock climbing”,
“interests”: [ “sports”, “music” ]
}
{
“first_name” : “Jane”,
“last_name” : “Smith”,
“age” : 32,
“about” : “I like to collect rock albums”,
“interests”: [ “music” ]
}
{
“first_name” : “Douglas”,
“last_name” : “Fir”,
“age” : 35,
“about”: “I like to build cabinets”,
“interests”: [ “forestry” ]
}
2.查看雇员id为1信息。
3.搜索所有雇员信息
4.搜索特定条件的雇员信息:通过非json形式搜索名字为Smith的雇员。
5.通过match的方式搜索名字为Smith的雇员。
6. 搜索名字为 Smith 的雇员,但年龄大于 30 岁的。
7. 搜索下所有喜欢攀岩(rock climbing)的雇员。
8. #仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语 “rock climbing” 的形式紧挨着的雇员记录
9.按照第8题的搜索要求,同时需要高亮显示搜索的内容。
二、商品信息查询。
1.请把以下文档导入ElasticSearch
{“id”: 1, “studentNo”: “TH-CHEM-2016-C001”, “name”: “Jonh Smith”, “major”:“Chemistry”, “gpa”: 4.8, “yearOfBorn”: 2000, “classOf”: 2016, “interest”: “soccer, basketball, badminton, chess”}
{“id”: 2, “studentNo”: “TH-PHY-2018-C001”, “name”: “Isaac Newton”, “major”:“Physics”, “gpa”: 3.6, “yearOfBorn”: 2001, “classOf”: 2018, “interest”: “novel, soccer, cooking”}
{“id”: 3, “studentNo”: “BU-POLI-2016-C001”, “name”: “John Kennedy”, “major”:“Politics”, “gpa”: 4.2, “yearOfBorn”: 2000, “classOf”: 2016, “interest”: “talking, dating, boxing, shooting, chess”}
{“id”: 4, “studentNo”: “BU-POLI-2015-C001”, “name”: “John Kerry”, “major”:“Politics”, “gpa”: 4.1, “yearOfBorn”: 1999, “classOf”: 2015, “interest”: “money, basketball”}
{“id”: 5, “studentNo”: “BU-ARTS-2016-C002”, “name”: “Da Vinci”, “major”:“Arts”, “gpa”: 4.8, “yearOfBorn”: 1995, “classOf”: 2016, “interest”: “drawing, music, wine”}
请查询
三、index操作
1.查询所有index列表,并将查询到的结果复制到此处。
2.创建website的index,要求为该索引有3个分片,2份副本。
3.删除website的index.
四、商品信息操作。
{ “index”: { “_id”: 1 }}
{ “price” : 10, “productID” : “XHDK-A-1293-#fJ3” }
{ “index”: { “_id”: 2 }}
{ “price” : 20, “productID” : “KDKE-B-9947-#kL5” }
{ “index”: { “_id”: 3 }}
{ “price” : 30, “productID” : “JODL-X-1937-#pV7” }
{ “index”: { “_id”: 4 }}
{ “price” : 30, “productID” : “QQPX-R-3956-#aD8” }
1, 将以上信息导入es,index为my_store,type为products。
2, 查找价格为20的商品信息,使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分
3, 查询具有"XHDK-A-1293-#fJ3"特定商品id的信息。
4, 查询价格在20-40之前的商品信息
5, 查找商品列表中价格为20或30的商品信息
6, 查询商品价格为30或者"productID"为"XHDK-A-1293-#fJ3"的商品信息,但是商品的"productID"不能为"QQPX-R-3956-#aD8"
7, 查询productID 为"KDKE-B-9947-#kL5"的商品信息或者 productID为"JODL-X-1937-#pV7" 并且同时 price为 30的商品信息
一、雇员表查询
PUT:192.168.56.100:9200/megacorp/employee/1
index为megacorp,type为employee 更改id即可
GET:192.168.56.100:9200/megacorp/employee/1
POST:192.168.56.100:9200/megacorp/_search
Body中写:
{
"query": {
"match_all": {}
}
}
{
"query": {
"multi_match": {
"query": "Smith",
"fields": ["last_name"]
}
}
}
{
"query": {
"match": {
"last_name" : "Smith"
}
}
}
{
"query": {
"bool": {
"must": {
"match": { "last_name" : "Smith"} },
"must_not": {
"range": { "age": { "lte": 30}}}
}
}
}
{
"query": {
"multi_match": {
"query": "climbing",
"fields": ["about"]
}
}
}
{
"query": {
"match_phrase":{
"about":"rock climbing"
}
}
}
{
"query": {
"match_phrase":{
"about":"rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
二、商品信息查询
1.批量导入
{"create":{"_index":"stu","_type":"doc","_id":"1"}}
{"id": 1, "studentNo": "TH-CHEM-2016-C001", "name": "Jonh Smith", "major":"Chemistry", "gpa": 4.8, "yearOfBorn": 2000, "classOf": 2016, "interest": "soccer, basketball, badminton, chess"}
{"create":{"_index":"stu","_type":"doc","_id":"2"}}
{"id": 2, "studentNo": "TH-PHY-2018-C001", "name": "Isaac Newton", "major":"Physics", "gpa": 3.6, "yearOfBorn": 2001, "classOf": 2018, "interest": "novel, soccer, cooking"}
{"create":{"_index":"stu","_type":"doc","_id":"3"}}
{"id": 3, "studentNo": "BU-POLI-2016-C001", "name": "John Kennedy", "major":"Politics", "gpa": 4.2, "yearOfBorn": 2000, "classOf": 2016, "interest": "talking, dating, boxing, shooting, chess"}
{"create":{"_index":"stu","_type":"doc","_id":"4"}}
{"id": 4, "studentNo": "BU-POLI-2015-C001", "name": "John Kerry", "major":"Politics", "gpa": 4.1, "yearOfBorn": 1999, "classOf": 2015, "interest": "money, basketball"}
{"create":{"_index":"stu","_type":"doc","_id":"5"}}
{"id": 5, "studentNo": "BU-ARTS-2016-C002", "name": "Da Vinci", "major":"Arts", "gpa": 4.8, "yearOfBorn": 1995, "classOf": 2016, "interest": "drawing, music, wine"}
命令:
curl -XPUT '192.168.56.100:9200/_bulk' -H 'Content-Type:application/json' --data-binary @di.json
查询:
1)同时查询id为1,3,5
{
"query":{
"terms":{
"id":["1","3","5"]
}
}
}
2)名字不叫John的文档
{
"query":{
"bool":{
"must_not":{
"match":{
"name":"John"
}
}
}
}
}
3)在2016年以前入学的文档
{
"query":{
"range":{
"classOf":{
"lte":"2015"
}
}
}
}
4)请把id为4文档添加一个兴趣 “poker”
PS:根据查询结果修改字段属性的值
{
"script": {
"source": "ctx._source['interset']='money, basketball,poker'",
"params": { "interest": "money, basketball, poker" },
"lang": "painless"
},
"query": {
"term": {
"id": "4"
}
}
}
结果如下:
{
"_index": "stu",
"_type": "doc",
"_id": "4",
"_version": 3,
"found": true,
"_source": {
"major": "Politics",
"interest": "money, basketball",
"yearOfBorn": 1999,
"name": null,
"gpa": 4.1,
"interset": "money, basketball,poker",
"studentNo": "BU-POLI-2015-C001",
"id": 4,
"classOf": 2015
}
}
三、index操作
1.查询所有index列表
192.168.56.100:9200/_mapping?pretty=true 格式化查询
{
"stu": {
"mappings": {
"doc": {
"properties": {
"classOf": {
"type": "long"
},
"gpa": {
"type": "float"
},
"id": {
"type": "long"
},
"interest": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"interset": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"major": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"studentNo": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"yearOfBorn": {
"type": "long"
}
}
}
}
},
"blog": {
"mappings": {
"article": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
},
"megacorp": {
"mappings": {
"employee": {
"properties": {
"about": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"first_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"interests": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"last_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"query": {
"properties": {
"match": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
},
"employee": {
"mappings": {
"em": {
"properties": {
"em_no": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sex": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
2.创建website的index,要求为该索引有3个分片,2份副本。
PUT:192.168.56.100:9200/website
{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":2
}
}
}
{
"website": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1561987828517",
"number_of_shards": "3",
"number_of_replicas": "2",
"uuid": "3aiQiakoQeGcnrfdBVqBjA",
"version": {
"created": "6020299"
},
"provided_name": "website"
}
}
}
}
3.删除website的index
DELETE:192.168.56.100:9200/website
四、商品信息操作
1,将以上信息导入es,index为my_store,type为products。
PUT 192.168.56.100:9200/mystore
{
"settings":{
"number_of_shards" : 1
},
"mappings":{
"products":{
"properties":{
"price":{
"type":"integer"
},
"productID":{
"type":"text"
}
}
}
}
}
PUT:192.168.56.100:9200/mystore/products/4
{
"price":30,
"productID":"QQPX-R-3956-#aD8"
}
依次根据信息插入就行
2.查找价格为20的商品信息,使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分
3.查询具有"XHDK-A-1293-#fJ3"特定商品id的信息
{
"query":{
"match":{
"productID" : "XHDK-A-1293-#fJ3"
}
}
}
4.查询价格在20-40之间的商品信息
{
"query":{
"range":{
"price" : {
"gte":20,
"lte":40
}
}
}
}
5.查找商品列表中价格为20或30的商品信息
{
"query":{
"terms":{
"price":[20,30]
}
}
}
6.查询商品价格为30或者"productID"为"XHDK-A-1293-#fJ3"的商品信息,但是商品的"productID"不能为"QQPX-R-3956-#aD8"
使用should
{
"query":{
"bool":{
"should":[
{
"match":{
"price":20
}},
{"match":{
"productID":"XHDK-A-1293-#fJ3"
}
}
]
,
"must_not":{
"match":{
"productID":"QQPX-R-3956-#aD8"
}
}
}
}
}
7.查询productID 为"KDKE-B-9947-#kL5"的商品信息或者 productID为"JODL-X-1937-#pV7" 并且同时 price为 30的商品信息
{
"query":{
"bool":{
"should":[
{
"match":{
"productID":"KDKE-B-9947-#kL5"
}},
{"match":{
"productID":"JODL-X-1937-#pV7"
}
}
]
,
"must":{
"match":{
"price":20
}
}
}
}
}