目录
前言
Elasticsearch可以作为数据库,但它最常用的也是最擅长的还是搜索引擎。本篇介绍elasticsearch搜索数据的DSL(Domain Specific Language)语法。部分内容总结摘抄自《Elasticsearch实战》,仅作笔记。另外,本篇文章基于es7.x,因此类型统一为_doc。
一、搜索前的准备
为了方便后面执行DSL语句以及可视化索引信息,我们使用kibana和head工具。首先使用kibana执行下面的语句创建一个主分片数量为1且副本分片数量为0的索引。
PUT /index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"gender": {
"type": "keyword"
},
"birthday":{
"type": "date",
"format": "yyyy-MM-dd"
},
"personalIntro":{
"type": "text"
}
}
}
}
执行完之后在head插件中就可以新增了一个名为index的索引,点击查看索引信息如下,可以看到与我们创建的映射一致。
然后向该索引中添加5条数据。
POST /index/_doc/
{
"name":"charles hahaha",
"age":18,
"gender":"male",
"birthday":"1991-10-22",
"personalIntro":"I am a handsome boy."
}
POST /index/_doc/
{
"name":"charles kakakakak",
"age":8,
"gender":"male",
"birthday":"1991-10-22",
"personalIntro":"Nice to meet you!"
}
POST /index/_doc/
{
"name":"krystal hahahahahhahahh",
"age":28,
"gender":"female",
"birthday":"1994-10-22",
"personalIntro":"I am a handsome girl,my name is charles hahahahahahaaaaaa."
}
POST /index/_doc/
{
"name":"charles wawawawawa",
"age":18,
"gender":"female",
"birthday":"1991-10-22",
"personalIntro":"I am a beautiful girl,my name is krystal hahahahahaha"
}
POST /index/_doc/
{
"name":"krystal wawawawawa",
"age":48,
"gender":"female",
"birthday":"1981-10-22",
"personalIntro":"I am a beautiful girl,what is your name?"
}