ElasticSearch语句

/test 为索引名称

一、创建索引

PUT /test

PUT 创建请求 

二、删除索引

DELETE /test

DELETE 删除请求

三、查询索引数据结构

GET /test

四、查询索引数据

GET /test/_search

GET 查询请求 /_search 执行搜索操作


#文本与结构化数据查询

#match为文本查询,当字段有设置分词器时会进行相应的分词操作

#term为结构化数据查询,term查询将按照储存再到排序引中的确切字词进行操作,这些查询通常用于数字,日期和枚举等结构化数据,而不是全文本字段

GET /test/_search
{
  "query":{
    "term": {
      "id": 1
    }
  }
}
GET /test/_search
{
  "query":{
    "match": {
      "name": "王"
    }
  }
}

#_source 指定查询的字段

GET /test/_search
{
  "query":{
    "match": {
      "name": "王"
    }
  }
  , "_source": ["name"]
}

#排序与分页

#form为查询的开始,size为查询的个数,sort为排序 age年龄字段 order:desc 为倒序排序

GET /test/_search
{
  "query": {
    "match": {
      "name": "王"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 20
}

#布尔查询

#must相当于 and

GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "王"
          }
        },
        {
          "term": {
            "age": 21
          }  
        }
      ]
    }
  }
}

#should相当于 or

GET /test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "age": 22
          }
        },
        {
          "term": {
            "age": 21
          }  
        }
      ]
    }
  }
}

#must_not相当于 not

GET /test/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "age": 21
          }
        }
      ]
    }
  }  
}

#过滤与多条件查询

#gt大于,gte大于等于,lt小于,lte小于等于

GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "王"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 21
            }
          }
        }
      ]
    }
  }
}

#keyword和text

#keyword不会被分词器拆分,text类型会

GET _analyze
{
  "analyzer": "keyword",
  "text": "老王"
}
GET _analyze
{
  "analyzer": "standard",
  "text": "老王"
}

#高亮

#fields:高亮的字段,pre_tags/post_tags 前后标签,优选使用字段内的高亮设置

GET /test/_search
{
  "query": {
    "match": {
      "name": "王"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>",
    "fields": {
      "name": {
         "pre_tags": "<p class='key' style='color:blue'>",
         "post_tags": "</p>"
      }
    }
  }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值