其他分享
首页 > 其他分享> > ES 花样查询

ES 花样查询

作者:互联网

二话不说上代码

 #创建索引库并指定索引类型
 PUT /test
 {
   "mappings": {
     "properties": {
       "name":{
         "type": "text"
       },
       "age":{
         "type": "integer"
       },
       "birthday":{
         "type": "date"
       }
     }
   }
 }
 
 #添加一条文档
 PUT /test/_doc/1
 {
   "name":"彼岸舞",
   "age":12,
   "birthday":"1999-11-11"
 }
 
 #暴力修改
 PUT /test/_doc/1
 {
   "name":"彼岸舞的博客",
   "age":12,
   "birthday":"1999-11-11"
 }
 
 #更新修改
 POST /test/_doc/1/_update
 {
   "doc":{
     "age":11
   }
 }
 
 #删除索引库
 DELETE test
 
 #创建索引库并添加数据
 PUT /dance/_doc/1
 {
   "name":"蓝贝儿",
   "age":18,
   "desc":"不明所以的小迷糊",
   "tags":["小仙女","小吃货","时尚达人"]
 }
 
 PUT /dance/_doc/2
 {
   "name":"夏天",
   "age":26,
   "desc":"小清新,萌萌的",
   "tags":["萌萌的","小吃货","暖男"]
 }
 
 PUT /dance/_doc/3
 {
   "name":"彼岸舞",
   "age":18,
   "desc":"走投无路的弟弟",
   "tags":["技术宅","二次元","码农"]
 }
 
 PUT /dance/_doc/4
 {
   "name":"彼岸花",
   "age":18,
   "desc":"走投无路的妹妹",
   "tags":["小仙女","妖娆","小吃货"]
 }
 
 PUT /dance/_doc/5
 {
   "name":"彼岸草",
   "age":3,
   "desc":"走投无路的姐姐",
   "tags":["御姐","时尚达人","大懒货"]
 }
 
 
 #根据ID查询
 
 GET dance/_doc/_search?q=name:彼岸舞
 
 #指定字段匹配
 
 GET dance/_doc/_search
 {
   "query":{
     "match":{
       "name":"彼岸"
     }
   }
 }
 
 #查询指定字段
 
 GET dance/_doc/_search
 {
   "query":{
     "match":{
       "name":"彼岸"
     }
   },
   "_source":["name","desc"]
 }

#指定字段排序
GET dance/_doc/_search
{
  "query": {
    "match": {
      "name": "彼岸"
    }
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

#分页查询
GET dance/_doc/_search
{
  "query": {
    "match": {
      "name": "彼岸"
    }
  },
  "from": 0,
  "size": 1
}

#多条件匹配 and关联
GET dance/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "彼岸"
          }
        },
        {
          "match": {
            "age": 3
          }
        }
      ]
    }
  }
}

#多条件匹配 or关联
GET dance/_doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "彼岸舞"
          }
        },
        {
          "match": {
            "age": 18
          }
        }
      ]
    }
  }
}

#多条件匹配 取非
GET dance/_doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "age": 18
          }
        }
      ]
    }
  }
}

#范围 lt 小于 lte 小于等于 gt 大于 gte 大于等于
#多条件匹配 数据过滤
GET dance/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "彼岸"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "lt": 10
          }
        }
      }
    }
  }
}

#空格分割多条件
GET /_search
{
  "query": {
    "match": {
      "tags": "小仙女 小吃货"
    }
  }
}

#精确查询
#term 查询是直接通过倒排索引指定的词条进行精确查找
#关于分词
#term 直接查询精确的
#match 会使用分词器(先分析文档,然后通过分析的文档进行查询)
#两个类型(重点)
#text keyword
#text 会被分词器解析 keyword不会被分词器解析

#创建索引库指定规则
PUT testdb
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "desc":{
        "type": "keyword"
      }
    }
  }
}

#添加测试数据

PUT testdb/_doc/1
{
  "name":"彼岸舞的博客园 java name",
  "desc":"彼岸舞的博客园 java desc"
}

PUT testdb/_doc/2
{
  "name":"彼岸舞的博客园 java name",
  "desc":"彼岸舞的博客园 java desc code"
}

#尝试分词器
GET _analyze
{
  "analyzer": "keyword",
  "text": "彼岸舞的博客园 java name"
}

GET _analyze
{
  "analyzer": "standard",
  "text": "彼岸舞的博客园 java name"
}

#检索测试 会被分词
GET testdb/_search
{
  "query": {
    "term": {
      "name": "java"
    }
  }
}

#不会被分词
GET testdb/_search
{
  "query": {
    "term": {
      "desc": "彼岸舞的博客园 java desc code"
    }
  }
}

#高亮查询
GET testdb/_search
{
  "query": {
    "match": {
      "name": "彼岸舞"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

#自定义高亮包裹标签
GET testdb/_search
{
  "query": {
    "match": {
      "name": "彼岸舞"
    }
  },
  "highlight": {
    "pre_tags": "<span class='key' style='color:red;'>",
    "post_tags": "</span>", 
    "fields": {
      "name": {}
    }
  }
}

自己去试,从上往下

作者:彼岸舞

时间:2020\09\10

内容关于:ElasticSearch

本文来源于网络,只做技术分享,一概不负任何责任

标签:name,花样,GET,dance,doc,彼岸,查询,match,ES
来源: https://www.cnblogs.com/flower-dance/p/13647730.html