1. 程式人生 > >Elasticsearch Query Language

Elasticsearch Query Language

官網連結

Elasticsearch 提供了JSON風格的特定領域語言,你可以用來執行自己的查詢

1.query索引中所有文件

curl 'localhost:9200/bank/_search?pretty' -d '
{
    "query":{"match_all":{}}
}

返回結果:
{
  "took" : 63,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score"
: 1.0, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "25", "_score" : 1.0, "_source" : { "account_number" : 25, "balance" : 40540, "firstname" : "Virginia", "lastname" : "Ayala", "age" : 39, "gender" : "F", "address"
: "171 Putnam Avenue", "employer" : "Filodyne", "email" : "[email protected]", "city" : "Nicholson", "state" : "PA" } }, { "_index" : "bank", "_type" : "account", "_id" : "44", "_score" : 1.0, "_source" : { "account_number"
: 44, "balance" : 34487, "firstname" : "Aurelia", "lastname" : "Harding", "age" : 37, "gender" : "M", "address" : "502 Baycliff Terrace", "employer" : "Orbalix", "email" : "[email protected]", "city" : "Yardville", "state" : "DE" } }, .... } ] } } ruby程式碼: require "elasticsearch" $elastic = Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true res = $elastic.search index:'bank',body:{query:{match_all:{}}} puts res

2.elasticsearch 預設返回文件的前十條資料,在查詢得時候可以指定想要返回的文件數量

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match_all": {} },
  "size": 1
}'

只返回索引中第一條文件
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "25",
      "_score" : 1.0,
      "_source" : {
        "account_number" : 25,
        "balance" : 40540,
        "firstname" : "Virginia",
        "lastname" : "Ayala",
        "age" : 39,
        "gender" : "F",
        "address" : "171 Putnam Avenue",
        "employer" : "Filodyne",
        "email" : "[email protected]",
        "city" : "Nicholson",
        "state" : "PA"
      }
    } ]
  }
}

ruby程式碼:
require "elasticsearch"
$elastic =  Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search  index:'bank',body:{query:{match_all:{}},size:1}
puts res

3.返回索引中第11-20條文件(from: 開始位置 size:從開始位置返回的文件數)

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}'

返回結果
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "227",
      "_score" : 1.0,
      "_source" : {
        "account_number" : 227,
        "balance" : 19780,
        "firstname" : "Coleman",
        "lastname" : "Berg",
        "age" : 22,
        "gender" : "M",
        "address" : "776 Little Street",
        "employer" : "Exoteric",
        "email" : "[email protected]",
        "city" : "Eagleville",
        "state" : "WV"
      }
    },
   .....(省略9條)
    } ]
  }
}

ruby程式碼:
require "elasticsearch"
$elastic =  Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search  index:'bank',body:{query:{match_all:{}},from:10,size:10}
puts res

4.按照年齡大小排序,返回年齡最大的Top3

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match_all": {} },
  "sort": { "age": { "order": "desc" } },
  "size":3
}'
返回結果:
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : null,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "948",
      "_score" : null,
      "_source" : {
        "account_number" : 948,
        "balance" : 37074,
        "firstname" : "Sargent",
        "lastname" : "Powers",
        "age" : 40,
        "gender" : "M",
        "address" : "532 Fiske Place",
        "employer" : "Accuprint",
        "email" : "[email protected]",
        "city" : "Umapine",
        "state" : "AK"
      },
      "sort" : [ 40 ]
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "40",
      "_score" : null,
      "_source" : {
        "account_number" : 40,
        "balance" : 33882,
        "firstname" : "Pace",
        "lastname" : "Molina",
        "age" : 40,
        "gender" : "M",
        "address" : "263 Ovington Court",
        "employer" : "Cytrak",
        "email" : "[email protected]",
        "city" : "Silkworth",
        "state" : "OR"
      },
      "sort" : [ 40 ]
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "468",
      "_score" : null,
      "_source" : {
        "account_number" : 468,
        "balance" : 18400,
        "firstname" : "Foreman",
        "lastname" : "Fowler",
        "age" : 40,
        "gender" : "M",
        "address" : "443 Jackson Court",
        "employer" : "Zillactic",
        "email" : "[email protected]",
        "city" : "Wakarusa",
        "state" : "WA"
      },
      "sort" : [ 40 ]
    } ]
  }
}

ruby程式碼:
require "elasticsearch"
$elastic =  Elasticsearch::Client.new hosts: [ 'localhost:9200' ], randomize_hosts: true
res = $elastic.search  index:'bank',body:{query:{match_all:{}},sort:{"age":{order:"desc"}}}
puts res

相關推薦

Elasticsearch Query Language

官網連結 Elasticsearch 提供了JSON風格的特定領域語言,你可以用來執行自己的查詢 1.query索引中所有文件 curl 'localhost:9200/bank/_search?pretty' -d ' { "query":{

HQL: The Hibernate Query Language

mount tca bytecode esc little ice des find times Chapter 14. HQL: The Hibernate Query Language 14.1. Case Sensitivity14.2. The from

elasticsearch query dsl

自己 中一 條件 tag ras detail 基礎上 fin 滿足 1.match / match_phrase / match_phrase_prefix / multi_match【查詢】 1.1 match 它會根據所給的字符串,進行分詞,然後去找出,包含這些分詞

Hibernate查詢(HQL——Hibernate Query Language

增加 exceptio 連接查詢 有一個 子句 內容 ike 例子 範圍 HQL查詢   HQL提供了是十分強大的功能,它是針對持久化對象,用取得對象,而不進行update,delete和insert等操作。而且HQL是面向對象的,具備繼承,多態和關聯等特性。 from子句

Hibernate Query Language查詢:

Hibernate Query Language查詢: Criteria查詢對查詢條件進行了面向物件封裝,符合程式設計人員的思維方式,不過HQL(Hibernate Query Language)查詢提供了更加豐富的和靈活的查詢特性,因此Hibernate將HQL查詢方式立為官方推薦的標準查詢方式,

HQL(Hibernate Query Language)

HQL是Hibernate Query Language的縮寫,提供更加豐富靈活、更為強大的查詢能力;HQL更接近SQL語句查詢語法。     按照條件查詢封裝資料有三種處理方式 1.VO 從建立一個新的實體類,新實體類的屬性就是當前HQL返回的資料 &n

Elasticsearch Query DSL 整理總結(二)—— 要搞懂 Match Query,看這篇就夠了

目錄 引言 構建示例 match operator 引數 analyzer lenient 引數 Fuzziness fuzzniess 引數 什麼是模糊搜尋? Levenshtein Edit Dist

NoSQL(not only struts query language)的簡單介紹

為什麼需要NoSQL? 網際網路自擴大規模來一直面臨3個問題 1.High performance高併發 一個網站開發實時生成動態頁面可能會存在高併發請求的需求,硬碟IO已經無法接受 2.Huge Storage高儲存 使用者資料太多,業務變複雜(類似qq,淘寶的功能變多),就連bat公司等也面臨高

Elasticsearch Query DSL 整理總結(三)—— Match Phrase Query 和 Match Phrase Prefix Query

目錄 引言 Match Phase Query slop 引數 analyzer 引數 zero terms query Match Phrase 字首查詢 max_expansions 小結 參考文件 系列文章列表

elasticsearch Query DSL(三)

本文參考官方提供api提煉出來的 https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html Compound Queries 複合查詢包裝其他複合或葉子查詢,以組合其結果和分數,更改其行為,或從

elasticsearch Query DSL(二)

本文參考官方提供api提煉出來的 https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html Term Level Queries Term Query term query查詢包含倒排索引中

elasticsearch Query DSL(一)

本文參考官方提供api提煉出來的 https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html Query DSL Elasticsearch提供基於JSON的完整查詢DSL(域特定語言)來定

elasticsearch-query-tookit一款基於SQL查詢elasticsearch程式設計工具包,支援SQL解析生成DSL,支援JDBC驅動,支援和Spring、MyBatis整合

  `elasticsearch-query-tookit`是一款基於SQL查詢elasticsearch程式設計工具包,支援SQL解析生成DSL,支援JDBC驅動,支援和Spring、MyBatis整合,提供Java程式設計介面可基於此工具包二次開發  只是重新造了個輪子,

powerful text query language

GraphQuery GraphQuery is a query language and execution engine tied to any backend service. It is back-end language indepen

Elasticsearch Query DSL 整理總結(一)—— Query DSL 概要,MatchAllQuery,全文查詢簡述

目錄 引言 雖然之前做過 elasticsearch 的專案,但是沒有對整個專案的知識點進行過系統的整理。這次趁著對 elasticsearch 版本的升級的機會(從2.2 升級到 6.3) ,又專門花時間對涉及到的知識點重新梳理了一遍。 俗話說,好記性不如爛筆頭。為了加深對 elasticsearch 的

Elasticsearch Query DSL 整理總結(二)—— 要搞懂 Match Query,看這篇就夠了

目錄 引言 昨天是感恩節,上幼兒園的女兒在老師的叮囑下,晚上為我和老婆洗了腳(形式上的^_^),還給我們每人端了一杯水。看著孩子一天天的長大,懂事,感覺很開心,話說咱們程式設計師這麼辛苦是為了什麼?不就是為了老婆,孩子,熱炕頭,有一個溫暖幸福的家庭,再捎帶著用程式碼改變一下世界嗎?想到這裡,頓時覺得學習,創

Elasticsearch Query DSL備忘(1)(Constant score query和Bool Query

Query DSL (Domain Specific Language),基於json的查詢方式 1、Constant score query,常量分值查詢,目的就是返回指定的score,一般都結合filter使用,因為filter context忽略score。 GET /customer/_se

JPQL查詢語句(Java Presistence Query Language)(轉)

1.Query createQuery(String qlString)  根據JPA的查詢語句建立一個查詢物件Query,如下面的程式碼:  Query q= em.createQuery(""SELECT t FROM Topic t WHERE t.topicTitle L

揭密首個面向IaaS的查詢語言:ZStack Query Language(ZQL)

為了簡化UI工作併為運維人員提供一種更加靈活的資源查詢方式,ZStack在2.6版本中推出了首個面向IaaS的查詢語言 —— ZStack Query Language,簡稱ZQL。 背景 IaaS管理著海量的資料中心資源,如何對這些資源進行靈活快速的查

CQL(Cassandra Query Language)函式

CREATE OR REPLACE FUNCTION averageState(state tuple<int,bigint>, val int) CALLED ON NULL INPUT RETURNS tuple LANGUAGE java AS $$