关于文档的基本操作

关于文档的基本操作

基本操作

添加数据

PUT /latte2/user/1
{
    
    
  "name":"latte",
  "age": 23,
  "desc": "干",
  "tags":["技术","海贼王"]
}
PUT /latte2/user/2
{
    
    
  "name": "张三",
  "age": 28,
  "desc": "法外狂徒",
  "tags": ["旅游", "渣男", "交友"]
}

PUT /latte2/user/3
{
    
    
  "name": "李四",
  "age": 30,
  "desc": "不知道怎么描述",
  "tags": ["旅游", "男", "唱歌"]
}

GET latte2/user/1


GET latte2/user/_search?q=name:latte

在这里插入图片描述

获取数据 GET

在这里插入图片描述
在这里插入图片描述

更新数据 PUT

在这里插入图片描述
put如果字段传不全,会把原来的字段丢弃,put也就是覆盖操作

Post _update , 推荐使用这种更新方式!

使用 POST 后面没有加 _update

在这里插入图片描述

其他没有被提交的字段会被置空,然后新添加了doc.name字段

在这里插入图片描述

使用 POST 后面加 _update

在这里插入图片描述

提交的字段值被更新了,没有被提交的字段还是原来的值

在这里插入图片描述

简单地搜索

GET latte2/user/1

简答的条件查询,可以根据默认的映射规则,产生基本的查询!

在这里插入图片描述
权重

复杂操作搜索

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

# 模糊查询
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  }
}

# 对查询结果进行字段过滤
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "_source": ["name", "desc"]
}

# 排序
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "sort":[{
    "age": "asc"
  }]
}

第二种写法
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

# 分页
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "sort":[{
    "age": "asc"
  }], 
  "from": 0,
  "size": 2
}

在这里插入图片描述
json的结构体
hit:索引和文档的信息。查询的总数,然后就是查询出来的具体的文档,数据中的东西都可以遍历出来了,分数:我们可以通过来的判断谁更加符合结果

输出结果,不想要那么多!
在这里插入图片描述

我们之后使用Java操作es ,所有的方法和对象就是这里面的 key!

排序!

在这里插入图片描述

分页查询!

在这里插入图片描述

数据下标还是从0开始的,和学的所有数据结构是一样的!

/search/{current}/{pagesize}

布尔值查询

# 多条件查询 must 相当于and
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "latte2"
        }},
        {"match": {
          "age": 23
        }}
      ]
    }
  }
}

# 多条件查询 should 相当于or
GET latte2/user/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "name": "latte2"
        }},
        {"match": {
          "age": 25
        }}
      ]
    }
  }
}

# 多条件查询 must_not 相当于 not
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "age": 25
        }}
      ]
    }
  }
}


# 过滤查询1 age > 27
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "latte2"
        }}
      ],
      "filter": [
        {"range": {
          "age": {
            "gt": 27
          }
        }}
      ]
    }
  }
}

# 过滤器2  22<age<30 
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "latte2"
        }}
      ],
      "filter": [
        {"range": {
          "age": {
            "lt": 30,
            "gt": 22
          }
        }}
      ]
    }
  }
}
GET latte2/user/_search
{
  "query": {
    "match": {
      "tags": "技术 男"
    }
  }
}

must (and),所有的条件都要符合 where id = 1 and name = xxx

在这里插入图片描述

should(or),所有的条件都要符合 where id = 1 or name = xxx

在这里插入图片描述

must_not (not)

在这里插入图片描述

过滤器 filter

在这里插入图片描述

  • gt 大于
  • gte 大于等于
  • lt 小于
  • lte 小于等于!

在这里插入图片描述

在这里插入图片描述

匹配多个条件!

在这里插入图片描述

精确查询!

# 定义类型term: 精确匹配
PUT latte3
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "desc": {
        "type": "keyword"
      }
    }
  }
}

PUT /latte3/_doc/1
{
  "name":"latte3",
  "desc":"latte3 desc"
}

PUT /latte3/_doc/2
{
  "name":"latte3",
  "desc":"desc 2"
}

# 按照keyword类型精准匹配
GET latte3/_search
{
  "query": {
    "term": {
      "desc": {
        "value": "desc 2"
      }
    }
  }
}
# 结果:
{
  "took" : 174,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "latte3",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "latte3",
          "desc" : "desc 2"
        }
      }
    ]
  }
}


# 按照text类型匹配
GET latte3/_search
{
  "query": {
    "term": {
      "name":"latte3"
    }
  }
# 结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "latte3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "latte3",
          "desc" : "latte3 desc"
        }
      },
      {
        "_index" : "latte3",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "latte3",
          "desc" : "desc 2"
        }
      }
    ]
  }
}
多个值匹配精确查询
PUT /latte3/_doc/3
{
  "t1":"22",
  "t2":"2021-03-13"
}

PUT /latte3/_doc/4
{
  "t1": "33",
  "t2": "2021-03-13"
}

GET /latte3/_search
{
  "query": {
   "bool": {
     "should": [
       {
         "term": {
           "t1": "22"
         }
       },{
         "term": {
           "t1": "33"
         }
       }
     ]
   }
  }
}
高亮
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>", 
    "fields": {
      "name": {}
    }
  }
}

# 结果显示:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 148,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.8405091,
    "hits" : [
      {
        "_index" : "latte2",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.8405091,
        "_source" : {
          "doc" : {
            "name" : "latte2"
          },
          "name" : "latte2"
        },
        "highlight" : {
          "name" : [
            "<p class='key' style='color:red'>latte2</p>"
          ]
        }
      },
      {
        "_index" : "latte2",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.8405091,
        "_source" : {
          "name" : "latte2",
          "age" : 28,
          "desc" : "法外狂徒",
          "tags" : [
            "旅游",
            "渣男",
            "交友"
          ]
        },
        "highlight" : {
          "name" : [
            "<p class='key' style='color:red'>latte2</p>"
          ]
        }
      }
    ]
  }
}

term 查询是直接通过倒排索引指定的词条进程精确查找的!

关于分词:

  • term ,直接查询精确的
  • match,会使用分词器解析!(先分析文档,然后在通过分析的文档进行查询!)

两个类型 text keyword (text类型的会被分词,keyword类型的不会被分词)

在这里插入图片描述

在这里插入图片描述

多个值匹配精确查询

在这里插入图片描述

高亮查询!

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43803285/article/details/114803961
今日推荐