基本操作
1、添加数据
PUT /test01/user/1
{
"name": "孙笑川",
"age": 33,
"birth": "2021-05-10",
"tags": ["抽象","带师兄"]
}
2、获取数据
GET /test01/user/1
3、更新数据 PUT
4、POST 更新(推荐使用)
5、搜索
简单搜索
复杂搜索
select(排序,分页,高亮,模糊查询,精准查询)
//查询参数体使用json构建
GET test01/user/_search
{
"query": {
"match": {
"name": "孙笑川"
}
}
}
结果过滤:
排序:
GET test01/user/_search
{
"query": {
"match": {
"name": "孙笑川"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
分页:
//from等同于pageNum
//size等同于pageSize
GET test01/user/_search
{
"query": {
"match": {
"name": "孙笑川"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 5
}
布尔值多条件查询:
must (and) 所有的条件都要匹配 where id = 1 and age = 10
should (or) 条件匹配 where id = 1 or age = 10
must_not (not)
过滤器 filter
gt # 大于
gte # 大于等于
lt # 小于
lte # 小于等于
匹配多个条件
精确查询
term 通过倒排索引指定的词条进行精确查询
分词
- term 直接精确查询
- match 查询时会使用分词器解析(先分析文档,再通过分析的文档进行查询)
两个类型 text(会被分词器解析) keyword(不会被分词器解析)
PUT test02
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"desc": {
"type": "keyword"
}
}
}
}
PUT test02/_doc/1
{
"name": "text keyword字段类型测试",
"desc": "text keyword字段类型测试 desc"
}
PUT test02/_doc/2
{
"name": "text keyword字段类型测试",
"desc": "text keyword字段类型测试 desc02"
}
GET _analyze
{
"analyzer": "keyword",
"text": "text keyword字段类型测试"
}
GET _analyze
{
"analyzer": "standard",
"text": "text keyword字段类型测试"
}
GET test02/_search
{
"query": {
"term": {
"name": "测"
}
}
}
GET test02/_search
{
"query": {
"term": {
"desc": "text keyword字段类型测试 desc"
}
}
}
PUT test02/_doc/3
{
"t1": "11",
"t2": "2021-05-11"
}
PUT test02/_doc/4
{
"t1": "22",
"t2": "2021-05-11"
}
GET test02/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "11"
}
},
{
"term": {
"t1": "22"
}
}
]
}
}
}
多个值匹配的精确查询
高亮查询
自定义高亮格式
评论 (0)