关于文档的基本操作

suaxi
2021-05-12 / 0 评论 / 244 阅读 / 正在检测是否收录...
基本操作

1、添加数据

PUT /test01/user/1
{
  "name": "孙笑川",
  "age": 33,
  "birth": "2021-05-10",
  "tags": ["抽象","带师兄"]
}


2、获取数据

GET /test01/user/1


3、更新数据 PUT

1.更新数据.png


4、POST 更新(推荐使用)

2.POST更新.png


5、搜索

简单搜索

3.简单搜索.png


复杂搜索

select(排序,分页,高亮,模糊查询,精准查询)

//查询参数体使用json构建
GET test01/user/_search
{
  "query": {
    "match": {
      "name": "孙笑川"
    }
  }
}

4.hit参数说明.png


结果过滤:

5.结果过滤.png


排序:

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

6.布尔值must多条件查询.png


should (or) 条件匹配 where id = 1 or age = 10

7.布尔值should多条件查询.png


must_not (not)

8.布尔值must_not条件查询.png


过滤器 filter

9.布尔值过滤filter查询.png

gt         # 大于
gte     # 大于等于

lt         # 小于
lte     # 小于等于


匹配多个条件

10.匹配多个条件.png


精确查询

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"
          }
        }
      ]
    }
  }
}

11.keyword类型没有被分析.png


12.被分词器拆分了.png


13.keyword没有被分词器解析.png


多个值匹配的精确查询

14.多个值精确查询.png


高亮查询

15.高亮显示.png

自定义高亮格式

16.自定义高亮格式.png

0

评论 (0)

取消