ElasticSearch6.2.4(3)——简单的搜索方式

1.准备工作,添加数据
PUT /zoo/product/1
{
  "name":"monkey",
  "age":10,
  "content":"xiao small but ke ai"
}

PUT /zoo/product/2
{
  "name":"monkey",
  "age":13,
  "content":"xiao small but very big"
}

PUT /zoo/product/3
{
  "name":"dog",
  "age":13,
  "content":"xiao big but very small"
}

PUT /zoo/product/4
{
  "name":"cat",
  "age":18,
  "content":"xiao big but very small"
}

PUT /zoo/product/5
{
  "name":"tig",
  "age":30,
  "content":"xiao big but very big"
}

2.搜索zoo下面product的所有document

GET /zoo/product/_search
took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高

hits.hits:包含了匹配搜索的document的详细数据

3.查询所有动物

GET /zoo/product/_search
{
  "query": {
    "match_all": {}
  }
}

4.查询包含big的内容,并且按照age降序(desc是降序,asc是升序)

GET /zoo/product/_search
{
  "query": {
    "match": {
      "content": "big"
    }
  },
  "sort": [
    {
        "age": {
          "order": "desc"
      }
    }
  ]
}

5.从0下标开始查询出2包含big的动物

GET /zoo/product/_search
{
  "query": {
    "match": {
      "content": "big"
    }
  },
  "size": 2,
  "from": 0
}

6.查询出来的数据只要age和content属性

GET /zoo/product/_search
{
  "_source": ["age","content"]
}

7.查询出包含big but整段短语

GET /zoo/product/_search
{
  "query": {
    "match_phrase": {
      "content": "big but"
    }
  }
}

8.查询出来的数据进行高亮处理

GET /zoo/product/_search
{
  "query": {
    "match": {
      "content": "big but"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值