1. 程式人生 > >elasticsearch檢索文件

elasticsearch檢索文件

一、 新增文件

第一個業務需求就是儲存僱員資料。 這將會以 僱員文件 的形式儲存:一個文件代表一個僱員。儲存資料到 Elasticsearch 的行為叫做 索引 ,但在索引一個文件之前,需要確定將文件儲存在哪裡。

一個 Elasticsearch 叢集可以 包含多個 索引 ,相應的每個索引可以包含多個 型別 。 這些不同的型別儲存著多個 文件 ,每個文件又有 多個 屬性 。

對於僱員目錄,我們將做如下操作:

  • 每個僱員索引一個文件,包含該僱員的所有資訊。
  • 每個文件都將是 employee 型別 。
  • 該型別位於 索引 megacorp 內。
  • 該索引儲存在我們的 Elasticsearch 叢集中。

進行下一步前,讓我們增加員工資訊到目錄中:

注意,路徑 /megacorp/employee/1 包含了三部分的資訊:

megacorp

索引名稱

employee

型別名稱

1

特定僱員的ID

請求體 —— JSON 文件 —— 包含了這位員工的所有詳細資訊,他的名字叫 John Smith ,今年 25 歲,喜歡攀巖。

很簡單!無需進行執行管理任務,如建立一個索引或指定每個屬性的資料型別之類的,可以直接只索引一個文件。Elasticsearch 預設地完成其他一切,因此所有必需的管理任務都在後臺使用預設設定完成。

 

curl -X PUT "localhost:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
'
curl -X PUT "localhost:9200/megacorp/employee/2" -H 'Content-Type: application/json' -d'
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
'
curl -X PUT "localhost:9200/megacorp/employee/3" -H 'Content-Type: application/json' -d'
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}
'

二、 檢索文件

1.檢索單個僱員的資料。這在 Elasticsearch 中很簡單。簡單地執行 一個 HTTP GET 請求並指定文件的地址——索引庫、型別和ID。使用這三個資訊可以返回原始的 JSON 文件

[[email protected] ~]#curl -X GET "localhost:9200/megacorp/employee/1"
{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source":
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
}

2. 我們使用下列請求來搜尋所有僱員

[[email protected] ~]#curl -X GET "localhost:9200/megacorp/employee/_search"
{"took":556,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":1.0,"_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":1.0,"_source":
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
},{"_index":"megacorp","_type":"employee","_id":"3","_score":1.0,"_source":
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}
}]}}

可以看到,我們仍然使用索引庫 megacorp 以及型別 employee`,但與指定一個文件 ID 不同,這次使用 `_search 。返回結果包括了所有三個文件,放在陣列 hits 中。

3.搜尋姓氏為 ``Smith`` 的僱員。這個方法一般涉及到一個 查詢字串 (_query-string_) 搜尋,因為我們通過一個URL引數來傳遞查詢資訊給搜尋介面,我們仍然在請求路徑中使用 _search 端點,並將查詢本身賦值給引數 q= 。

[[email protected] ~]#curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith"
{"took":4390,"timed_out":false,"_shards":{"total":5,"successful":5,"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" ]
}
}]}}

4.

Query-string 搜尋通過命令非常方便地進行臨時性的即席搜尋 ,但它有自身的侷限性(參見 輕量 搜尋 )。Elasticsearch 提供一個豐富靈活的查詢語言叫做 查詢表示式 , 它支援構建更加複雜和健壯的查詢。

領域特定語言 (DSL), 指定了使用一個 JSON 請求。我們可以像這樣重寫之前的查詢所有 Smith 的搜尋 :

[[email protected] ~]#curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
'
{"took":40,"timed_out":false,"_shards":{"total":5,"successful":5,"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" ]
}
}]}}