ElasticSearch的基本操作

增加

PUT index/type/1
{
	"name":"your name",
	"age":100
	"tags":"帅"
}

删除

DELETE index # 删除整个索引 等于删除整个数据库
DELETE index/type/1 # 删除某个文档

# 还可以使用POST删除,不推荐
POST index/type/_delete_by_query?q=name:'xxx'

更新

如果使用 PUT 将会更新整个文档内容

更新个别字段 需要使用POST

POST index/type/1
{
	"name":"new name"
}

查询

​ 两种查询方式:

  1. 字符串 query string
  2. DSL
GET index/type/_search #查询所有
GET index/type/_search
{
	"query":{
		"match_all":{}
	}
}  #查询所有

GET index/type/_search?q=name:"xxx" # query string 查询

GET index/type/_search
{
	"query":{
		"match":{
			"name":"xxx"
		}
	}
} # DSL查询

排序

GET index/type/_search
{
	"query":{
		"match_all":{}
	},
	"sort":{
		"age":{
			"order":"desc" # 还可以是asc,与mysql一样。
		}
	}
} 

分页

GET index/type/_search
{
	"query":{
		"match_all":{}
	},
	"from":0, # 从第一条开始,包含第一条数据
	"size":2, # 返回两条
}

GET index/type/_search
{
	"query":{
		"match_all":{}
	},
	"from":1, # 从第二条开始,包含第二条数据
	"size":2, # 返回两条
}

布尔查询

  • should
  • must
  • must_not

# 布尔查询:should(or) must(and) must_not(not)
# or
GET index/type/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "liunan5"
          }
        },
        {
          "match": {
            "age": "11"
          }  
        }
      ]
    }
  }
}

# 查询性别是男的,年龄是44的
# and
GET index/type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "age": 44
          }
        },
        {
          "match": {
            "sex": "男"
          }
        }
      ]
    }
  }
}


# 查询性别不是男的,年龄也不是55的
# not
GET index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "sex": "男"
          }
        },
        {
          "match": {
            "age": 55
          }
        }
      ]
    }
  }
}

比较运算

gt 大于

gte 大于等于

lt 小于

lte 小于等于

# 查询年龄大于20的男的文档
GET index/type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "sex": "男"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 22
          }
        }
      }
    }
  }
}

高亮查询

# 查询name是liunan1的文档,并高亮显示关键字
GET index/type/_search
{
  "query": {
    "match": {
      "name": "liunan1"
    }
  },
  "highlight": {
    "fields": {
      "name": {
        
      }
    }
  }
}

# 自定义高亮样式
GET index/type/_search
{
  "query": {
    "match": {
      "desc": "厉害"		# 关键字查询
    }
  },
  "highlight": {
    "pre_tags": "<b style='color:red;font-size:20px;'>", # 关键字前面的标签
    "post_tags": "</b>", 							# 关键字后面的标签
    "fields": {
      "desc":{}							 			# 需要高亮显示的字段
    }
  }
}

结果过滤

GET index/type/_search
{
  "query": {
    "match": {
      "name": "liunan1"
    }
  },
  "_source": ["name","age"]	# 过滤
}

聚合查询

  • SUM
  • MAX
  • MIN
  • AVG
# sum 查询所有的男生年龄和
GET index/type/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  },
  "aggs": {
    "my_sum": {
      "sum": {
        "field": "age"
      }
    }
  }
}


# max 查询最大的男生的年龄 
GET index/type/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  },
  "aggs": {
    "my_max": {
      "max": {
        "field": "age"
      }
    }
  }
}


# min 查询最小的男生的年龄 
GET index/type/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  },
  "aggs": {
    "my_min": {
      "min": {
        "field": "age"
      }
    }
  }
}


# avg 查询所有人的平均年龄
GET index/type/_search
{
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  }
}

分组

# 年龄10-20,20-30,30-100分组
GET index/type/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  },
  "aggs": {
    "my_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 100
          }
        ]
      }
    }
  }
}

分组后聚合

# 分组,年龄10-20,20-30,30-100分组
# 对每组年龄求和
GET index/type/_search
{
  "aggs": {
    "my_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 100
          }
        ]
      },
      "aggs": {
        "my_sum": {
          "sum": {
            "field": "age"
          }
        }
      }
    }
  }
}

mapping 映射

用 dynamic 来设置 . 有三种映射类型:

  • dynamic : true
  • dynamic : false
  • dynamic : strict

true : 默认的映射类型,不需要手动设置, 当向文档里添加了新字段时,会自动为该自动添加映射关系

false : 当 dynamic 设置为 false 时, 可以向文档里添加新字段 , 但是不会为添加的新字段建立映射关系. 新字段无法作为查询条件进行查询(查询也没有结果) , 新增的字段只能当作伴随字段 随查询结果一起返回.

strict : 当 dynamic 设置为 strict 时 , 不可以 向文档里添加新字段 , 添加会 报错

PUT s4
{
  "mappings": {
    "doc":{
      "dynamic":true,
      "properties":{
        "name":{
          "type":"text"
        }
      }
    }
  }
}
GET s4/_mapping

PUT s5
{
  "mappings": {
    "doc":{
      "dynamic":false,
      "properties":{
        "name":{
          "type":"text"
        }
      }
    }
  }
}
GET s5/_mapping

PUT s6
{
  "mappings": {
    "doc":{
      "dynamic":"strict",
      "properties":{
      "name":{
        "type":"text"
        }
      }
    }
  }
}
GET s6/_mapping

ignore_above

超过 ignore_above 设置的字符串将不会被索引 或者 存储 , 对于字符串数组, ignore_above 将分别应用于每一个数组元素 .

PUT s7
{
  "mappings": {
    "doc":{
      "properties":{
        "title":{
          "type":"keyword",
          "ignore_above":10 # 这里默认是256,可以自己设置
        }
      }
    }
  }
}

index

index 默认为 true , 如果设置成 false , es则不会为该属性建立索引, 也就是说, 无法当作主查询条件

PUT s8
{
  "mappings": {
    "doc":{
      "properties":{
        "t1":{
          "type":"text",
          "index":true
        },
        "t2":{
          "type":"text",
          "index":false
        }
      }
    }
  }
}


PUT s8/doc/1
{
  "t1":"好饿",
  "t2":"不是很饿"
}


GET s8/doc/_search
{
  "query": {
    "match": {
      "t1": "饿"
    }
  }
}

GET s8/doc/_search
{
  "query": {
    "match": {
      "t2": "饿" # 报错!
    }
  }
}

copy_to

PUT s9
{
  "mappings": {
    "doc":{
      "properties":{
        "t1":{
          "type":"text",
          "copy_to":"full_name" # 这里也可以是一个数组["full_name1","full_name2"]
        },
        "t2":{
          "type":"text",
          "copy_to":"full_name"
        },
        "full_name":{
          "type":"text"
        }
      }
    }
  }
}



PUT s9/doc/1
{
  "t1":"ooo",
  "t2":"xxx"
}

PUT s9/doc/2
{
  "t1":"oooxxx",
  "t2":"xxx"
}



GET s9/doc/_search
{
  "query": {
    "match": {
      "t1": "ooo"
    }
  }
}

GET s9/doc/_search
{
  "query": {
    "match": {
      "full_name": "xxx"
    }
  }
}

对象类型,嵌套

PUT w1
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"long"
        },
        "info":{
          "properties":{
            "addr":{
              "type":"text"
            },
            "tel":{
              "type":"long"
            }
          }
        }
      }
    }
  }
}


PUT w1/doc/1
{
  "name":"liunan",
  "age":19,
  "info":{
    "addr":"北京",
    "tel":110
  }
}

GET w1/doc/_search
{
  "query": {
    "match": {
      "info.addr": "北"
    }
  }
}

settings设置

设置主,复制分片

主分片默认是5

复制分片默认是1

主分片一经定义不可以修改,复制分片后期可以改。

PUT w2
{
  "mappings": {
    "doc":{
      "properties":{
        "title":{
          "type":"text"
        }
      }
    }
  }, 
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 3
  }
}

一个mapping示例

PUT w3
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"text",
          "index":true
        },
        "age":{
          "type":"long",
          "index":false
        },
        "desc":{
          "type":"keyword",
          "ignore_above":128,
          "copy_to":["t1","t2"]
        },
        "tags":{
          "type":"keyword",
          "index":false,
          "copy_to":["t1","t2"]
        },
        "info":{
          "properties":{
            "addr":{
              "type":"text"
            },
            "tel":{
              "type":"long"
            }
          }
        },
        "t1":{
          "type":"text"
        },
        "t2":{
          "type":"text"
        }
      }
    }
  },
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 3
  }
}

match 系列

关键字查询 match

  • 返回所有匹配的分词

match 查询的是散列映射,包含了我们希望搜索的字段和字符串。

也就是说,只要文档中 只要有 我们希望的那个关键字 ,就会返回结果。

关键字会被分词 之后进行查询

GET index/type/_search
{
	"query":{
		"match":{
			"name":"xxx"
		}
	}
} # DSL查询
# 标准的分析器
# 查看对中国的分析过程
GET _analyze
{
  "analyzer": "standard",
  "text":"中国"
}

# 分析结果,发现分析器将 “中国” 分词了
{
  "tokens" : [
    {
      "token" : "中",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "国",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    }
  ]
}

查询全部 match_all

  • 查询全部
GET index/type/_search
{
	"query":{
		"match_all":{}
	}
}

短语查询 match_phrase

  • 短语查询,在match 的基础上进一步查询词组,可以指定slop分词间隔。

将关键字 作为 短语 进行查询,不会对关键字进行分词后查询。

GET t1/doc/_search
{
  "query": {
    "match_phrase": {
      "title": "中国"
    }
  }  
}
# 这条查询只能查出 完整 包含 “中国” 关键字的结果。

短语分割

用slop来分割 短语

以下语句表示 允许 中国 世界 之间有一个间隔。

GET t1/doc/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "中国世界",
        "slop": 1
      }
    }
  }
}

最左前缀查询 match_phrase_prefix

  • 前缀查询,根据短语中最后一个词组做前缀匹配,可以应用于搜索提示
PUT t2/doc/1
{
  "title":"beautiful girl"
}

PUT t2/doc/2
{
  "title":"beautiful so"
}

GET t2/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "title": "so"
    }
  }
}

多字段查询 multi_match

  • 多字段查询,使用相当灵活,可以完成 match_phrase 和 match_phrase_prefix 的工作。
# 多字段联合查询
GET t2/doc/_search
{
  "query": {
    "multi_match": {
      "query": "beautiful",
      "fields": ["t1","t2"]
    }
  }
}
GET t2/doc/_search
{
  "query": {
    "multi_match": {
      "query": "beautiful",
      "fields": ["t1","t2"],
      "type":"phrase"  # 还可以phrase_prefix
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章