Elasticsearch検索
13350 ワード
検索データの作成
ElasticSearchの最も魅力的なところは、簡単で便利な検索機能を提供してくれます。まず以下のコマンドを使ってテスト文書を作成してみます。
ElasticSearchでの検索は、主にhttp://localhost:9200/_search...-すべてのIndexとType を検索します。http://localhost:9200/movies/... -Moviesインデックスのすべてのタイプを検索する http://localhost:9200/movies/movie... -Movies索引Movieタイプに含まれる文書のみを検索する 応答内容にはドキュメントのメタ情報が含まれます。元のデータは文書に存在します。ソースフィールドにあります
ある文書を検索しても文書の_を直接検索できます。sourceフィールドは以下の通りです。
場合によっては、ドキュメントの個々のフィールドを検索するだけで、_を使用することができます。sourceパラメータは、複数のフィールドでカンマ区切りが使用できます。以下の通りです。
検索には「godfather」というキーワードが含まれています。
参考:ElasticSearch 2.x入門と快速実践Elasticsearch入門
ElasticSearchの最も魅力的なところは、簡単で便利な検索機能を提供してくれます。まず以下のコマンドを使ってテスト文書を作成してみます。
curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'
curl -XPUT "http://localhost:9200/movies/movie/2" -d'
{
"title": "Lawrence of Arabia",
"director": "David Lean",
"year": 1962,
"genres": ["Adventure", "Biography", "Drama"]
}'
curl -XPUT "http://localhost:9200/movies/movie/3" -d'
{
"title": "To Kill a Mockingbird",
"director": "Robert Mulligan",
"year": 1962,
"genres": ["Crime", "Drama", "Mystery"]
}'
curl -XPUT "http://localhost:9200/movies/movie/4" -d'
{
"title": "Apocalypse Now",
"director": "Francis Ford Coppola",
"year": 1979,
"genres": ["Drama", "War"]
}'
curl -XPUT "http://localhost:9200/movies/movie/5" -d'
{
"title": "Kill Bill: Vol. 1",
"director": "Quentin Tarantino",
"year": 2003,
"genres": ["Action", "Crime", "Thriller"]
}'
curl -XPUT "http://localhost:9200/movies/movie/6" -d'
{
"title": "The Assassination of Jesse James by the Coward Robert Ford",
"director": "Andrew Dominik",
"year": 2007,
"genres": ["Biography", "Crime", "Drama"]
}'
ここで知るべきことは、ElasticSearchが私たちに共通のauを提供してくれたことです。Blkエンドポイントは、単一要求において複数のドキュメントの作成操作を完了するために使用されるが、ここでは、単純化のために複数の要求に分けて実行される。ElasticSearchでの検索は、主に
_search
というエンドポイントに基づいて行われ、その標準的な要求フォーマットは、//_search
であり、indexとtypeは任意である。言い換えれば、私たちは次のような方法で請求を開始することができます。ある文書を検索しても文書の_を直接検索できます。sourceフィールドは以下の通りです。
curl -XGET 'http://localhost:9200/movies/movie/1/_source'
返した結果:{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}
すべての文書を検索します。私たちは_を使うことができます。searchというAPIは、すべてのドキュメントを検索します。コマンドは以下の通りです。curl -XGET 'http://localhost:9200/movies/movie/_search'
返した結果:{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "5",
"_score": 1,
"_source": {
"title": "Kill Bill: Vol. 1",
"director": "Quentin Tarantino",
"year": 2003,
"genres": [
"Action",
"Crime",
"Thriller"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "2",
"_score": 1,
"_source": {
"title": "Lawrence of Arabia",
"director": "David Lean",
"year": 1962,
"genres": [
"Adventure",
"Biography",
"Drama"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "4",
"_score": 1,
"_source": {
"title": "Apocalypse Now",
"director": "Francis Ford Coppola",
"year": 1979,
"genres": [
"Drama",
"War"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "6",
"_score": 1,
"_source": {
"title": "The Assassination of Jesse James by the Coward Robert Ford",
"director": "Andrew Dominik",
"year": 2007,
"genres": [
"Biography",
"Crime",
"Drama"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_score": 1,
"_source": {
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": [
"Crime",
"Drama"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "3",
"_score": 1,
"_source": {
"title": "To Kill a Mockingbird",
"director": "Robert Mulligan",
"year": 1962,
"genres": [
"Crime",
"Drama",
"Mystery"
]
}
}
]
}
}
hits
というobject
は、total
、hits
の配列などのフィールドを含み、hits
は、すべてのドキュメントを含んでいます。ここでは2つのドキュメントしかありません。total
は、ドキュメントの数を示しています。デフォルトでは、最初の10の結果に戻ります。From/Size
パラメータを設定して、ある範囲のドキュメントを取得することもできます。ここを参照してください。curl -XGET 'http://localhost:9200/movies/movie/_search?from=1&size=2'
返した結果は以下の通りです。{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "2",
"_score": 1,
"_source": {
"title": "Lawrence of Arabia",
"director": "David Lean",
"year": 1962,
"genres": [
"Adventure",
"Biography",
"Drama"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "4",
"_score": 1,
"_source": {
"title": "Apocalypse Now",
"director": "Francis Ford Coppola",
"year": 1979,
"genres": [
"Drama",
"War"
]
}
}
]
}
}
いくつかのフィールドを検索場合によっては、ドキュメントの個々のフィールドを検索するだけで、_を使用することができます。sourceパラメータは、複数のフィールドでカンマ区切りが使用できます。以下の通りです。
curl -XGET 'http://localhost:9200/movies/movie/1?_source=title,director'
返した結果:{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"director": "Francis Ford Coppola",
"title": "The Godfather"
}
}
query string検索query string検索はq=field:value
という形式で照会しています。例えば、クエリーtitle
フィールドはgodfather
の映画を含んでいます。curl -XGET 'http://localhost:9200/movies/movie/_search?q=title:godfather'
返した結果:{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25811607,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_score": 0.25811607,
"_source": {
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": [
"Crime",
"Drama"
]
}
}
]
}
}
DSL検索上のquery string検索は比較的軽量級で、簡単な場合にのみ適用されます。Elasticsearchはより強力なDSL(Domain Specific Language)クエリ言語を提供しており、全文検索などの複雑な検索シーンに適しています。上記のquery string検索をDSL検索に変換できます。GET /movies/movie/_search
{
"query" : {
"match" : {
"title" : "godfather"
}
}
}
curlの使用要求:curl -X GET "127.0.0.1:9200/movies/movie/_search" -d '{"query": {"match": {"title": "godfather"}}}'
一番簡単な検索要求は全文検索です。例えば、ここで検索キーワードが必要です。godfather:検索には「godfather」というキーワードが含まれています。
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "godfather",
}
}
}'
titleで「godfather」というキーワードを検索します。curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "godfather",
"fields": ["title"]
}
}
}'
返した結果:{
"took": 24,
"timed_out": false,
"_shards": {
"total": 25,
"successful": 25,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25811607,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_score": 0.25811607,
"_source": {
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": [
"Crime",
"Drama"
]
}
}
]
}
}
文書が存在するかどうかを確認します。文書が存在するかを確認するだけです。内容には全く興味がありません。HEAD要求は応答体に戻りません。HTTPヘッダのみです。curl -i -XHEAD "http://localhost:9200/movies/movie/3"
Elasticsearchは200 OK状態に戻ります。文書が存在する場合:HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 255
存在しない場合は404 Not Foundに戻ります。curl -i -XHEAD "http://localhost:9200/movies/movie/36"
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 60
もちろん、これはお問い合わせの時点ではドキュメントが存在しないという意味ですが、数ミリ秒後も存在しないという意味ではありません。他のプロセスはこの期間中に新しい文書を作成することができます。参考:ElasticSearch 2.x入門と快速実践Elasticsearch入門