微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【ElasticSearch】前缀搜索,通配符搜索,正则搜索

数据准备

//创建索引
PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "title": {
          "type": "keyword"
        }
      }
    }
  }
}
//新增数据
POST my_index/my_type
{
  "title":"C4I8-UI365"
}
POST my_index/my_type
{
  "title":"C3K5-DFG65"
}
POST my_index/my_type
{
  "title":"C4I8-UI365"
}
//前缀搜索
GET my_index/my_type/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "C3"
      }
    }
  }
}
//通配搜索
GET my_index/my_type/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "C5"
      }
    }
  }
}
//正则搜索
GET my_index/my_type/_search
{
  "query": {
    "regexp": {
      "title": {
        "value": "C[0-9].+"
      }
    }
  }
}

 

注意:

  1.以上三种搜索性能都很低,会扫描所有的倒排索引才会结束

  2.前缀越短,要处理的doc越多,性能越差,尽可能用长前缀搜索

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐