Elasticsearch实战 第四章 搜索数据

2021-11-28  本文已影响0人  kaiker
搜索请求路由

1、搜索请求的结构

搜索范围

搜索范围

请求的基本模块

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"
    ]
}'

回复的结构

回复的结构

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"}
      }
    }
  }
}'

常用的基础查询和过滤器

curl -XPOST 'localhost:9200/get-together/_search?pretty' -d '{
  "query": {
    "query_string":{
      "query":"nosql"
    }
  }
}'
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"
        }
      }
    }
  }
}'
curl 'localhost:9200/get-together/group/_search' -d '{
  "query": {
    "terms":{
      "tags":["elasticsearch","jvm"]
    }
  },
  "_source":["name", "tags"]
}'

match查询和term过滤器

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查询

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

exists过滤器

curl  'localhost:9200/get-together/event/_search' -d '{
  "query": {
    "filtered":{
      "query":{
        "match_all":{}
       },
       "filter":{
          "exists":{"field":"location.geolocation"}
       }
    }
  }
}

missing过滤器

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、为任务选择查询

查询案例
上一篇 下一篇

猜你喜欢

热点阅读