java代码实现搜索elasticsearch索引数据

1、关于获取客户端以及增删改操作请看上一篇文章,文章中有详细的讲解

2、本篇文章讲述关于elasticsearch搜索匹配方式,主要有 match、match_phrase、term、regexp、wildcard、range和分页以及排序的方法

  (1)、分页查询数据,对结果进行排序完整格式为

{
    "from" :0, "size" :20,
    "query": {
          "match_all":{}
    },"sort": [{ "排序字段": { "order": "desc" }}]
} 

  (2)、各种查询匹配格式(重要部分)

  match:分词匹配   "match": { "epNo" : "我是人" }  //匹配epNo包含 '我'   ‘是’   ‘人’   的数据,会将一个整的语句拆成一个字一个字的

  match_phrase:不分词匹配      "match_phrase": { "epNo" : "123456" }  //匹配epNo包含123456的数据

  term:完全匹配     "term": { "epNo" : "123456" }  //匹配epNo等于123456的数据

  regexp:正则匹配     "regexp": { "epNo" : "*123456*" }  //匹配epNo包含123456的数据

  wildcard:表达式匹配    "wildcard": { "epNo" : "*123456*" }  //匹配epNo包含123456的数据

  range:区间范围匹配    "range": { "goodsPrice": { "gte":20,"lte":50 } }  //匹配价钱在20~40中间的数据

  (3)、should的使用

  为了保证should中的条件至少有一个存在,语法如下

  "must": [
    {
      "bool": {
          "should": [
          { "match_phrase": { "goodsSubtitle": { "query" : "游戏", "boost" : 5 }}},
          { "match_phrase": { "goodsName": { "query" : "游戏", "boost" : 5 }}}
          ]
        }
    }
  ]

 

posted @ 2018-08-14 16:56  lazy_cxy  阅读(964)  评论(0编辑  收藏  举报