es搜索-Search API

Search API

es 提供了兩種搜索方式:字符串查詢(Query String Syntax)搜索DSL 查詢

Query String Syntax

Query string 搜索通過命令非常方便地進行臨時性地即席搜索,但它有自身的侷限性

· 使用 q 指定查詢字符串

· Query String Syntax:K/V 鍵值對

示例如下:

<code>GET http://localhost:9200/_search?q=last_name:Smith/<code>

查詢的結果如下:

<code>{ "took": 156, "timed_out": false, "num_reduce_phases": 2, "_shards": { "total": 666, "successful": 557, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.2876821, "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.2876821, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } } ] }}/<code>

DSL Query【推薦使用】

ElasticSearch 針對 Query-string 搜索的侷限性,提供了一個非常豐富靈活的查詢語言叫做查詢表達式,它支持構建更加複雜和健壯的查詢。

領域特定語言(DSL),使用 JSON 構建一個請求。示例如下:

<code>GET http://192.168.186.50:9200/_searchContent-Type: application/json;charset=UTF-8{ "query": { "match": { "last_name": "Smith" } }}/<code>

查詢到的結果跟使用 Query-string 的結果是一樣的,此處就不貼結果了。


分享到:


相關文章: