美文网首页
Elasticsearch实战 第四章 搜索数据

Elasticsearch实战 第四章 搜索数据

作者: kaiker | 来源:发表于2021-11-28 12:47 被阅读0次
搜索请求路由

1、搜索请求的结构

搜索范围

  • _search端点展开搜索
搜索范围

请求的基本模块

  • query 主体模块
  • size 返回文档数量
  • from 和size一起使用,用于分页
  • _source 指定source字段如何返回
  • sort 指定排序依据,默认按照得分进行排序
curl 'localhost:9200/get-together/_search?from=10&size=10'
curl 'localhost:9200/get-together/_search?sort=date:asc&_source=title,data'
curl 'localhost:9200/get-together/_search?sort=date:asc&q=title:elasticsearch'

基于请求主体的搜索请求

curl 'localhost:9200/get-together/_search' -d '{
  "query": {
    "match_all":{}
  },
  "_source":["name", "date"]
}'

curl 'localhost:9200/get-together/_search' -d '{
  "query": {
    "match_all":{}
  },
  "_source":["name*", "date"]
}'

curl 'localhost:9200/get-together/_search' -d '{
  "query": {
    "match_all":{}
  },
  "sort":[
      {"created_on":"asc"},
      {"name":"desc"},
       "_score"
    ]
}'

回复的结构

回复的结构
  • 如果没有存储文档的_source或fields,将无法从ES中获取数值

2、查询和过滤器DSL

match查询和term过滤器

curl 'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "match":{"title":"hadoop"}
  }
}'
普通查询和过滤器查询
  • 过滤器查询是不计算得分的,可以被缓存,速度也快
curl 'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match":{"title":"hadoop"}
      },
      "filter": {
        "term":{"host":"andy"}
      }
    }
  }
}'
  • 过滤器的底层由ES建立了一个位集合,标识某个文档是否匹配过滤器,然后缓存过滤器的意思也是缓存这个位图


    过滤器结果位图缓存

常用的基础查询和过滤器

  • match_all 匹配所有文档
  • query_string 默认搜索_all字段,但是也可以指定字段;它还可以传表达式
curl -XPOST 'localhost:9200/get-together/_search?pretty' -d '{
  "query": {
    "query_string":{
      "query":"nosql"
    }
  }
}'
  • term和term过滤器 指定需要搜索的文档字段和词条
curl 'localhost:9200/get-together/group/_search' -d '{
  "query": {
    "term":{
      "tags":"elasticsearch"
    }
  },
  "_source":["name", "tags"]
}'
curl 'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match_all":{}
      },
      "filter": {
        "terms":{
          "tags":"elasticsearch"
        }
      }
    }
  }
}'
  • terms查询
  • 默认只要有一个匹配就会返回
curl 'localhost:9200/get-together/group/_search' -d '{
  "query": {
    "terms":{
      "tags":["elasticsearch","jvm"]
    }
  },
  "_source":["name", "tags"]
}'

match查询和term过滤器

  • 往name里传的不是string,是映射


    match查询
    词组查询

phrase_prefix查询

  • 和同词组最后一个词条进行前缀匹配
curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "match":{
      "name":{
        "type":"phrase_prefix",
        "query":"es den",
        "max_expansions"
      }
    }
  },
  "_source":["name"]
}'

3、组合查询和复合查询

bool查询

子句类型
curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "bool":{
      "must":[
        {
          "term": {
            "attendees":"david"
          }
        }
      ]
    }
  }
}'

bool过滤器

curl 'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match_all":{}
      },
      "filter": {
        "bool":{
          "must":[ {
            "term":{
              "attendees":"client"
             }
             }
          ]
        }
      }
    }
  }
}'

4、超越match和过滤器查询

range查询和过滤

  • 可以获取一定范围中的值
curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "range":{
      "created_on": {
        "gt":"2012-06-01",
        "lt":"2012-09-01"
      }
    }
  }
}
range参数

prefix

  • 允许根据给定的前缀来搜索词条,这里前缀在搜索之前没经过分析
curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "prefix":{
      "title":"liber"
    }
  }
}

wildcard查询

  • 可以加入正则


    waildcard

5、使用过滤器查询字段存在性

exists过滤器

  • 可以查找字段有值的文档
curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match_all":{}
       },
       "filter":{
          "exists":{"field":"location.geolocation"}
       }
    }
  }
}

missing过滤器

  • 搜索字段里没有值
  • existence 参数控制字段是否存在, null_value字段匹配是否字段为null的
curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match_all":{}
       },
       "filter":{
          "missing":{"field":"reviews"}
       }
    }
  }
}

将任何查询转换为过滤器

curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match_all":{}
       },
       "filter":{
          "query":{
            "query_string": {
               "query": "name:\"denver clojure\""
             }
           }
       }
    }
  }
}

6、为任务选择查询

查询案例

相关文章

网友评论

      本文标题:Elasticsearch实战 第四章 搜索数据

      本文链接:https://www.haomeiwen.com/subject/mwcqxrtx.html