ElasticSearch 关于文档的基本操作

PUT /test1/type1/1
{
  "name": "是阿离啊",
  "age": 18
}
PUT /test2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age":{
        "type": "long"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}

GET test2

PUT /test3/_doc/1
{
  "name": "花花",
  "age": 18,
  "birth": "2001-08-17"
}

GET test3

GET _cat/health

GET _cat/indices?v
POST /test3/_doc/1/_update
{
  "doc":{
    "name": "张三"
  }
}
DELETE test2

关于文档的基本操作

基本操作

添加一条数据:PUT

PUT /li/user/1
{
  "name": "是阿离啊",
  "age": 18,
  "desc": "你也想要个甜甜的恋爱吗",
  "tags":["没有对象?","单生狗?","一直努力学习吧"]
}

 

 创建第二个用户:

PUT /li/user/2
{
  "name": "张三",
  "age": 23,
  "desc": "法外狂徒张三",
  "tags":["紧急避险","投币未遂","请点赞"]
}

获取数据:GET

GET li/user/1

 

更新数据:PUT

PUT /li/user/3
{
  "name": "依依",
  "age": 24,
  "desc": "这个人很神秘没有描述",
  "tags":["你想知道","就不告诉你","请点赞"]
}

 version代表被改动的次数 创建的时候为1

第二种方法:使用POST 后跟_update进行修改(推荐使用这种更新方式)

POST li/user/2
{
  "doc":{
    "name": "还带是你"
  }
}

但是你会发现这样写会导致表中数据丢失

 所以要加_update字段

POST li/user/2/_update
{
  "doc":{
    "name": "还带是你"
  }
}

 

 

简单的搜索:

GET li/user/1

 简单的条件查询 (如果查出多个试试把name后面要查的值带引号)

GET li/user/_search?q=name:"是阿离啊"

 

复杂操作 select (排序,分页,高亮....)

我们可以通过_source来限制查询出来的结果:

GET li/user/_search
{
  "query": {
    "match": {
      "name": "离"
    }
  },
  "_source": ["name","desc"]
}

 排序:

GET li/user/_search
{
  "query": {
    "match": {
      "name": "离"
    }
  },
  "_source": ["name","desc"],
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

 分页:

GET li/user/_search
{
  "query": {
    "match": {
      "name": "离"
    }
  },
  "_source": ["name","desc"],
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 1
}

 

GET li/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "阿离"
          }
        },{
          "match": {
            "age": "20"
          }
        }
      ]
    }
  }
}

or 

GET li/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "阿离"
          }
        },{
          "match": {
            "age": "20"
          }
        }
      ]
    }
  }
}

GET li/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "阿离"
          }
        }
      ]
    }
  }
}

 gt 大于 >

lt 小于<

gte 大于等于 >=

lte 小于等于 <=

GET li/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "阿离"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 18
          }
        }
      }
    }
  }
}

可以使用读个条件过滤 

 只要满足其中一个都会被查出 这个时候可以通过分值进行过滤

精确查询

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

关于分词:

term ,直接查询精确的

match ,会使用分词器解析(线分析文档。然后在通过分析的文档进行查询)

两个类型:text   keyword

keyword

text    

 

 总结:keyword字段类型不会被分词器解析

多个值精确查询

 

GET li/user/_search
{
  "query": {
    "match": {
      "tags": "单 对象 啊"
    }
  }
}

PUT testdb
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "desc": {
        "type": "keyword"
      }
    }
  }
}

PUT testdb/_doc/1
{
  "name": "是阿离啊 name",
  "desc": "是阿里啊 desc"
}

PUT testdb/_doc/2
{
  "name": "是阿离啊 name",
  "desc": "是阿里啊 desc2"
}

GET _analyze
{
  "analyzer": "keyword",
  "text": "是阿离啊 name"
}
GET _analyze
{
  "analyzer": "standard",
  "text": "是阿离啊 name"
}
GET testdb/_search
{
  "query": {
    "term": {
      "name": "离"
    }
  }
}
GET testdb/_search
{
  "query": {
    "term": {
      "desc": "离"
    }
  }
}

PUT testdb/_doc/3
{
  "t1":"22",
  "t2":"2022-5-3"
}
PUT testdb/_doc/3
{
  "t1":"33",
  "t2":"2022-5-4"
}

GET testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "t1": "22"
          }
        },{
          "term": {
            "t1": "33"
          }
        }
      ]
    }
  }
}

高亮查询:

 

 

GET li/user/_search
{
  "query": {
    "match": {
      "name": "阿离"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>", 
    "post_tags": "</p>", 
    "fields": {
      "name":{}
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值