1.通过elasticsearch-head 创建

(1)登录localhost:9100

(2)点击复合查询

(3)输入内容

 elasticsearch创建索引

(4)勾选易读,点击验证是否是JSON格式

(5)点击提交请求,返回

{

  • "acknowledged": true

}

2.通过postman来创建索引:

(1)选择请求格式PUT,输入请求访问地址:127.0.0.1:9200/peoper

(2)选择下面的Body->raw->JSON(application/json)

(3)创建索引,例如:

{
 "settings":{
  "number_of_shards":3, //创建分片数
  "number_of_replicas":1//创建备份数
 },
 "mappings":{
  "man":{
   "properties":{
    "name":{
     "type":"text"
    },
    "country":{
     "type":"keyword"
    },
    "age":{
     "type":"integer"
    },
    "data":{
     "type":"date",
     "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    
    }
   }
  }
 }
}

(4)点击send ,如果格式正确会返回如下信息:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "peoper"
}

(5)现在索引就创建好了,返回elasticsearch-head 的页面刷新就能看到

 3.在postman中对索引进行插入数据

(1)选择访问请求为POST ,请求内容:127.0.0.1:9200/peoper/man/(也可以在后面跟上ID号,不跟是自动自增长ID)

(2)根据创建索引是创建的数据格式,插入数据如下:

{
 "name":"王尼玛",
 "country":"China",
 "age":35,
 "date":"1987-12-08"
}

如国数据添加成功会返回信息如下:

elasticsearch创建索引

(3)在浏览器中点击刷新,就能够看到数据增加一条。点击数据浏览找到对应的索引就能看到插入数据的内容。

4.使用postman直接修改文档(指定修改文件的内容)

(1)请求访问类型为POST,请求访问内容输入:127.0.0.1:9200/peoper/man/1/_update(修改索引peoper下man对象ID为1的数据,后面的_update修改必须更上)

(2)修改内容如下:

{

 "doc":{  

 "name":"baing"  

}

}

其中修改的数据必须放在:“doc”:{}中

 5.使用脚本修改文档(使用postman)

(1)请求访问类型为POST,请求访问内容输入:127.0.0.1:9200/peoper/man/1/_update(修改索引peoper下man对象ID为1的数据,后面的_update修改必须更上)

(2)修改内容如下:

{  

"script":{   //使用脚本语言的类型

"lang":"painless",  //lang为语言,painless为内置的语言还可以是python

 "inline":"ctx._source.age += 15"  //获取当前年龄在加上15

 }

}

5.删除对应的数据

elasticsearch创建索引

 6.查询

(1)简单查询:

在postman中选择GET  内容为127.0.0.1:9200/peoper/man/1

(2)条件查询

类型选择POST 内容为:127.0.0.1:9200/peoper/_search

查询条件:

{
"query":{
 "match_all":{}
}
}

这样就查出所有的内容

 elasticsearch创建索引

图中“from”表示从第几条数据开始,“size”表示返回一条数据

 elasticsearch创建索引

表示查询出标题中含有“elasticsearch”的内容通过“publish_date”这个字段进行降序

(3)聚合查询

elasticsearch创建索引

图中"aggs"为聚合查询的关键自,"group_by_word_count"自定义根据字数查询的名字,“word_count”表示根据这个字段去查询统计

. 条件查询
{   
  "query":{
    "match":{
      "title":"elasticsearch"
    }
  },
  "from": 1,
  "size": 2,
  "sort":[{"publish_date":"desc"}]
}

match_all :表示查询所有 match : 表示条件查询 from : 表示返回结果从第几页开始 size : 表示返回结果的大小 sort : 表示排序

6. 聚合查询
{
  "aggs": {
    "group_by_word_count": {
      "terms":{
        "field":"word_count"
      }
    },
    "group_by_publish_date":{
      "terms":{
        "field":"publish_date"
      }
    }
  }
}

aggs: 表明是聚合查询 "group_by_word_count":自定义名称,可以随意 terms:关键字 field:使用的字段

7. 统计查询
{
"aggs": {
  "grand_word_count":{
    "stats":{
      "field":"word_count"
    }
  }
}
}

返回结果:

aggregations":{
"grand_word_count":{
"count": 8,
"min": 2000,
"max": 5000,
"avg": 3375,
"sum": 27000
}
}

说明: aggs:统计查询 grand_word_count:自定义名称 stats:统计方法,可以换成min/max/sum field:进行统计的字段

8. 高级查询

高级查询分为子条件查询和复合查询

1. 子条件查询:特定字段查询所指特定值
1. query context

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

常用查询:

  1. 全文本查询: 针对文本类型的查询

a. 模糊匹配:

​ post - http://127.0.0.1:9200/book/_search

{
  "query":{
    "match":{
      "title":"ElastichSearch入门"
    }
  }
}

​ 从结果中可以看出,结果会匹配ElasticSearch入门,他们的关系是或的关系,相当于自动分词

b. 习语匹配

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

从结果中可以看出,会把ElasticSearch入门当做一个整体的词进行匹配

c. 多个字段的模糊查询

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

d. querystring,语法查询()

{
  "query":{
    "query_string":{
      "query":"(ElasticSearch) AND 入门) OR Python"
    }
  }
}
{
  "query":{
    "query_string":{
      "query":"瓦力 OR ElasticSearch",
      "fields":["author","title"]
    }
  }
}

2). 字段级别的查询: 针对结构化数据,如数字,日期等

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

term : 表示具体的字段查询

还可以指定范围:

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

关键词:range表明是范围查询,后面跟具体的字段,gte表示>=,lte表示<=

范围,还可以用在日期上.

2. filter context

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

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

filter结合bool使用

2. 复合条件查询:以一定的逻辑组合子条件查询
1. 固定分数查询
{
  "query":{
    "constant_score":{
      "filter":{
        "match":{
          "title":"ElasticSearch"
        }
      },
      "boost":2
    }
  }
}

constant_score:固定分数,即把_score的值指定,如果不加boost则为1,指定了boost的值,则_score等于boost的值

注意: constant_score不支持match

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

should为关键词,应该满足他列出的条件,是或的关系

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

must:与的关系

must和filter

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

相关文章: