검색 명령어에 대해 알아보기 전에
이전에 나왔던 elastic search 용어에 대해서 다시 한번 더 보고 넘어가도록 하자.
검색 방법에는 두가지가 존재한다.
1. URI를 이용한 검색
- q(query)
- df(default field)
- default_operator
- _source
- fields
- sort
- from
2. 리퀘스트 바디 검색
- size, from, fields
- sort
- partial_fields, fielddata_fields
- highlight
자 이제 하나하나를 알아보고, 돌아와서 목차를 보면서 정리해보도록 하자.
0. 연습 data 받아오기
검색 실습을 하기 위해 데이터를 입력하겠다. 입력할 데이터 예제 파일은 깃허브에서 내려받았다.
https://github.com/wikibook/elasticsearch/blob/master/05.%EA%B2%80%EC%83%89/5_1_books.json
https://github.com/wikibook/elasticsearch/blob/master/05.%EA%B2%80%EC%83%89/5_2_magazines.json
접속하여 레파지토리에서 예제 파일을 다운로드받는다.
먼저 엘라스틱서치의 bulk API를 이용해서 5_1_books.json, 5_2_magazines.json 파일의 내용을 엘라스틱서치에 입력한다.
curl -XPOST localhost:9200/_bulk --data-binary @5_1_books.json --header 'content-type:application/json'
curl -XPOST localhost:9200/_bulk --data-binary @5_2_magazines.json --header 'content-type:application/json'
1.검색
엘라스틱서치의 검색 기능은 질의(query) 명령어를 이용해 수행된다.
*질의를 수행하는 방법은 2가지가 있다.
1) REST API
2) http 데이터를 이용한 request body 방식
1.1 검색(_search) API
엘라스틱서치에서 검색은 인덱스 또는 타입 단위로 수행된다.
실습1) books 인덱스의 book 타입에서 hamlet이라는 검색어로 검색을 수행한다.
- 검색은 _search API를 사용하며, 질의는 q 매개변수의 값으로 입력한다.
curl 'http://localhost:9200/books/book/_search?q=hamlet&pretty'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 4.278328,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "TGvKC3cBf7K4q_TS667E",
"_score" : 4.278328,
"_source" : {
"title" : "Hamlet",
"author" : "William Shakespeare",
"category" : "Tragedies",
"written" : "1599-06-01T12:34:00",
"pages" : 172,
"sell" : 146100000,
"plot" : "The protagonist of Hamlet is Prince Hamlet of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father's brother and successor. Claudius hastily married King Hamlet's widow, Gertrude, Hamlet's mother. Denmark has a long-standing feud with neighbouring Norway, and an invasion led by the Norwegian prince, Fortinbras, is expected."
}
}
]
}
}
실습2) 여러 인덱스 동시에 검색하기.
books 인덱스와 magazines 인게스에서 검색어 time으로 동시에 검색을 수행해보자.
* 멀티테넌시(multi tenancy) : 여러 인덱스를 동시에 검색하는 기능
- 사용 방법 : 검색할 인덱스를 쉽표(,)로 구분해서 입력하면 됨.
curl 'http://localhost:9200/books,magazines/_search?q=time&pretty'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6, // 총 6건의 데이터가 검색
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 2.5739596,
"_source" : {
"title" : "The Time Machine",
"author" : "H. G. Wells",
"category" : "Science fiction novel",
"written" : "1895-11-01T05:01:00",
"pages" : 227,
"sell" : 22100000,
"plot" : "The book's protagonist is an English scientist and gentleman inventor living in Richmond, Surrey in Victorian England, and identified by a narrator simply as the Time Traveller. The narrator recounts the Traveller's lecture to his weekly dinner guests that time is simply a fourth dimension, and his demonstration of a tabletop model machine for travelling through it. He reveals that he has built a machine capable of carrying a person, and returns at dinner the following week to recount a remarkable tale, becoming the new narrator."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "VmvKC3cBf7K4q_TS667E",
"_score" : 1.9426696,
"_source" : {
"title" : "Around the World in Eighty Days",
"author" : "Jules Verne",
"category" : "adventure novel",
"written" : "1873-07-01T10:30:00",
"pages" : 189,
"sell" : 27200000,
"plot" : "Fogg and Passepartout reach Suez in time. While disembarking in Egypt, they are watched by a Scotland Yard detective named Fix, who has been dispatched from London in search of a bank robber. Because Fogg matches the description of the robber, Fix mistakes Fogg for the criminal. Since he cannot secure a warrant in time, Fix boards the steamer conveying the travellers to Bombay. Fix becomes acquainted with Passepartout without revealing his purpose. Fogg promises the steamer engineer a large reward if he gets them to Bombay early. They dock two days ahead of schedule."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "VGvKC3cBf7K4q_TS667E",
"_score" : 1.5109229,
"_source" : {
"title" : "Twenty Thousand Leagues Under the Sea",
"author" : "Jules Verne",
"category" : [
"Science fiction",
"adventure novel"
],
"written" : "1870-06-01T10:34:00",
"pages" : 304,
"sell" : 78100000,
"plot" : "In the year 1866, ships of several nations spot a mysterious sea monster, which some suggest to be a giant narwhal. The United States government assembles an expedition in New York City to find and destroy the monster. Professor Pierre Aronnax, a French marine biologist and narrator of the story, who happens to be in New York at the time, receives a last-minute invitation to join the expedition which he accepts. Canadian master harpoonist Ned Land and Aronnax's faithful servant Conseil are also brought aboard."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "U2vKC3cBf7K4q_TS667E",
"_score" : 1.2768264,
"_source" : {
"title" : "The Invisible Man",
"author" : "H. G. Wells",
"category" : [
"Horror",
"Science fiction novel"
],
"written" : "1897-05-01T07:32:00",
"pages" : 210,
"sell" : 67800000,
"plot" : "A mysterious stranger, Griffin, arrives at the local inn of the English village of Iping, West Sussex, during a snowstorm. The stranger wears a long-sleeved, thick coat and gloves, his face hidden entirely by bandages except for a fake pink nose, and a wide-brimmed hat. He is excessively reclusive, irascible, and unfriendly. He demands to be left alone and spends most of his time in his rooms working with a set of chemicals and laboratory apparatus, only venturing out at night. While staying at the inn, hundreds of strange glass bottles arrive that Griffin calls his luggage. Many local townspeople believe this to be very strange. He becomes the talk of the village (one of the novel's most charming aspects is its portrayal of small-town life in southern England, which the author knew from first-hand experience)."
}
},
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WGvLC3cBf7K4q_TScK64",
"_score" : 1.2039728,
"_source" : {
"title" : "Time",
"company" : "Time Inc.",
"category" : "News magazine",
"issue" : "2014-03-01T00:00:00"
}
},
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WWvLC3cBf7K4q_TScK65",
"_score" : 0.6931471,
"_source" : {
"title" : "People",
"company" : "Time Inc.",
"category" : "Human interest",
"issue" : "2013-10-01T00:00:00"
}
}
]
}
}
※ _all을 사용하여 클러스터의 모든 인덱스에서 검색을 할 수 도 있다. 또는 인덱스 자체를 생략해도 된다.
하지만 전체 클러스터 검색은 성능을 저하시키고 의도하지 않은 데이터가 검색되는 등 검색결과의 오염이 발생할 수 있으므로 될 수 있으면 사용하지 않는 것을 권장한다.
1.2. URI 검색
엘라스틱서치의 두 가지 주요 검색 방법중 URI 검색을 먼저 살펴보자.
위 _search API의 실습은 모두 URI 검색 방법으로 진행했다.
1.2.1 q(query)
*q : 질의 명령을 입력하는 가장 기본적인 검색 매개변수
실습3) 특정 필드만 검색하기. title 필드에서 time을 검색해보자.
* 특정 필드만 검색하는 방법 : q={필드}:{검색어}
curl 'http://localhost:9200/books,magazines/_search?q=title:time&pretty'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 2.5739596,
"_source" : {
"title" : "The Time Machine",
"author" : "H. G. Wells",
"category" : "Science fiction novel",
"written" : "1895-11-01T05:01:00",
"pages" : 227,
"sell" : 22100000,
"plot" : "The book's protagonist is an English scientist and gentleman inventor living in Richmond, Surrey in Victorian England, and identified by a narrator simply as the Time Traveller. The narrator recounts the Traveller's lecture to his weekly dinner guests that time is simply a fourth dimension, and his demonstration of a tabletop model machine for travelling through it. He reveals that he has built a machine capable of carrying a person, and returns at dinner the following week to recount a remarkable tale, becoming the new narrator."
}
},
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WGvLC3cBf7K4q_TScK64",
"_score" : 1.2039728,
"_source" : {
"title" : "Time",
"company" : "Time Inc.",
"category" : "News magazine",
"issue" : "2014-03-01T00:00:00"
}
}
]
}
}
실습4) title 필드에 time과 machine이 동시에 들어간 값을 찾자.
- q 매개변수에 AND 또는 OR 값을 공백과 함께 넣어서 지정할 수 있다.
- 공백값은 인코딩 형식으로 입력해야 하므로 %20을 입력해야 한다.
curl 'http://localhost:9200/books,magazines/_search?q=title:time%20AND%20machine&pretty'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 6.232274,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 6.232274,
"_source" : {
"title" : "The Time Machine",
"author" : "H. G. Wells",
"category" : "Science fiction novel",
"written" : "1895-11-01T05:01:00",
"pages" : 227,
"sell" : 22100000,
"plot" : "The book's protagonist is an English scientist and gentleman inventor living in Richmond, Surrey in Victorian England, and identified by a narrator simply as the Time Traveller. The narrator recounts the Traveller's lecture to his weekly dinner guests that time is simply a fourth dimension, and his demonstration of a tabletop model machine for travelling through it. He reveals that he has built a machine capable of carrying a person, and returns at dinner the following week to recount a remarkable tale, becoming the new narrator."
}
}
]
}
}
1.2.2 df(default field)
- q 질의에 필드명을 넣는 대신 df 매개변수를 사용해서 검색할 필드를 지정할 수 있다.
- 실습3의 q=title:time 와 q=time&df=title 이 동일한 결과를 출력한다.
curl 'http://localhost:9200/books,magazines/_search?q=time&df=title&pretty'
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 2.5739596,
"_source" : {
"title" : "The Time Machine",
"author" : "H. G. Wells",
"category" : "Science fiction novel",
"written" : "1895-11-01T05:01:00",
"pages" : 227,
"sell" : 22100000,
"plot" : "The book's protagonist is an English scientist and gentleman inventor living in Richmond, Surrey in Victorian England, and identified by a narrator simply as the Time Traveller. The narrator recounts the Traveller's lecture to his weekly dinner guests that time is simply a fourth dimension, and his demonstration of a tabletop model machine for travelling through it. He reveals that he has built a machine capable of carrying a person, and returns at dinner the following week to recount a remarkable tale, becoming the new narrator."
}
},
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WGvLC3cBf7K4q_TScK64",
"_score" : 1.2039728,
"_source" : {
"title" : "Time",
"company" : "Time Inc.",
"category" : "News magazine",
"issue" : "2014-03-01T00:00:00"
}
}
]
}
}
1.2.3 default_operator
- q 매개변수에 조건 명령어 AND 와 OR를 질의로 사용할 수 있는 것을 확인했다.
조건 명령어를 지정하지 않고 공백으로 질의어를 나누면 OR로 인식한다. 다음 두 명령어는 같다.
1) /_search?q=title:time OR machine
2) /_search?q=title:time machine
default_operator 매개변수를 이용해서 기본 조건 명령어를 OR에서 AND로 변경할 수 있다.
어디에 유용하게 쓰이는건지는 아직 잘 모르겠다.
curl 'http://localhost:9200/books,magazines/_search?q=title:time%20machine&default_operator=AND&pretty'
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 6.232274,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 6.232274,
"_source" : {
"title" : "The Time Machine",
"author" : "H. G. Wells",
"category" : "Science fiction novel",
"written" : "1895-11-01T05:01:00",
"pages" : 227,
"sell" : 22100000,
"plot" : "The book's protagonist is an English scientist and gentleman inventor living in Richmond, Surrey in Victorian England, and identified by a narrator simply as the Time Traveller. The narrator recounts the Traveller's lecture to his weekly dinner guests that time is simply a fourth dimension, and his demonstration of a tabletop model machine for travelling through it. He reveals that he has built a machine capable of carrying a person, and returns at dinner the following week to recount a remarkable tale, becoming the new narrator."
}
}
]
}
}
1.2.4 explain
- explain 매개변수를 추가하면 각 검색 처리에 대해 해당 검색 결과의 점수 계산에 사용된 상세 값이 출력 결과에 표시된다.
- 점수는 해당 검색어에 대한 정확도를 계산한 값이며 점수가 높을수록 검색 결과 상위에 나타나게 된다.
책에는 그냥 explain을 매개변수로 넣으면 된다고 하는데 상세값이 나오지 않는다.
explain=true 까지 넣어줘야 그제서야 나온다. 책 내용대로 했다가 안되는 부분이 묘하게 많음..
curl 'http://localhost:9200/_search?q=title:time&explain=true&pretty'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_shard" : "[books][0]",
"_node" : "rWXs4zxCTx6G3uc2S-wo8Q",
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 2.5739596,
"_source" : {
"title" : "The Time Machine",
"author" : "H. G. Wells",
"category" : "Science fiction novel",
"written" : "1895-11-01T05:01:00",
"pages" : 227,
"sell" : 22100000,
"plot" : "The book's protagonist is an English scientist and gentleman inventor living in Richmond, Surrey in Victorian England, and identified by a narrator simply as the Time Traveller. The narrator recounts the Traveller's lecture to his weekly dinner guests that time is simply a fourth dimension, and his demonstration of a tabletop model machine for travelling through it. He reveals that he has built a machine capable of carrying a person, and returns at dinner the following week to recount a remarkable tale, becoming the new narrator."
},
"_explanation" : {
"value" : 2.5739596,
"description" : "weight(title:time in 10) [PerFieldSimilarity], result of:",
"details" : [
{
"value" : 2.5739596,
"description" : "score(freq=1.0), computed as boost * idf * tf from:",
"details" : [
{
"value" : 2.2,
"description" : "boost",
"details" : [ ]
},
{
"value" : 2.3671236,
"description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
"details" : [
{
"value" : 1,
"description" : "n, number of documents containing term",
"details" : [ ]
},
{
"value" : 15,
"description" : "N, total number of documents with field",
"details" : [ ]
}
]
},
{
"value" : 0.494263,
"description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
"details" : [
{
"value" : 1.0,
"description" : "freq, occurrences of term within document",
"details" : [ ]
},
{
"value" : 1.2,
"description" : "k1, term saturation parameter",
"details" : [ ]
},
{
"value" : 0.75,
"description" : "b, length normalization parameter",
"details" : [ ]
},
{
"value" : 3.0,
"description" : "dl, length of field",
"details" : [ ]
},
{
"value" : 3.7333333,
"description" : "avgdl, average length of field",
"details" : [ ]
}
]
}
]
}
]
}
},
{
"_shard" : "[magazines][0]",
"_node" : "rWXs4zxCTx6G3uc2S-wo8Q",
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WGvLC3cBf7K4q_TScK64",
"_score" : 1.2039728,
"_source" : {
"title" : "Time",
"company" : "Time Inc.",
"category" : "News magazine",
"issue" : "2014-03-01T00:00:00"
},
"_explanation" : {
"value" : 1.2039728,
"description" : "weight(title:time in 1) [PerFieldSimilarity], result of:",
"details" : [
{
"value" : 1.2039728,
"description" : "score(freq=1.0), computed as boost * idf * tf from:",
"details" : [
{
"value" : 2.2,
"description" : "boost",
"details" : [ ]
},
{
"value" : 1.2039728,
"description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
"details" : [
{
"value" : 1,
"description" : "n, number of documents containing term",
"details" : [ ]
},
{
"value" : 4,
"description" : "N, total number of documents with field",
"details" : [ ]
}
]
},
{
"value" : 0.45454544,
"description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
"details" : [
{
"value" : 1.0,
"description" : "freq, occurrences of term within document",
"details" : [ ]
},
{
"value" : 1.2,
"description" : "k1, term saturation parameter",
"details" : [ ]
},
{
"value" : 0.75,
"description" : "b, length normalization parameter",
"details" : [ ]
},
{
"value" : 1.0,
"description" : "dl, length of field",
"details" : [ ]
},
{
"value" : 1.0,
"description" : "avgdl, average length of field",
"details" : [ ]
}
]
}
]
}
]
}
}
]
}
}
1.2.5 _source
- _source 매개변수 값을 false로 설정하면 검색 결과에서 도큐먼트 내용은 표시하지 않고 메타 정보만 출력된다.
curl 'http://localhost:9200/_search?q=title:time&_source=false&pretty'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 2.5739596
},
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WGvLC3cBf7K4q_TScK64",
"_score" : 1.2039728
}
]
}
}
1.2.6 fields
- 출력 결과에 표시하고 싶은 필드만 별도 지정할 때 사용한다.
- RDB 테이블에서 select (column1),(column2).... 와 같은 기능인것 같다.
그런데 오류가 뜨면서 되지 않는다.
curl 'http://localhost:9200/_search?q=title:time&fields=title,author,category&pretty'
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "request [/_search] contains unrecognized parameter: [fields]"
}
],
"type" : "illegal_argument_exception",
"reason" : "request [/_search] contains unrecognized parameter: [fields]"
},
"status" : 400
}
fields --> _source로 변경되었다.
curl 'http://localhost:9200/_search?q=title:time&_source=title,author,category&pretty'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "UmvKC3cBf7K4q_TS667E",
"_score" : 2.5739596,
"_source" : {
"author" : "H. G. Wells",
"title" : "The Time Machine",
"category" : "Science fiction novel"
}
},
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "WGvLC3cBf7K4q_TScK64",
"_score" : 1.2039728,
"_source" : {
"title" : "Time",
"category" : "News magazine"
}
}
]
}
}
1.2.7 sort
- 검색 결과의 출력 순서를 정하는데 사용된다.
기본적으로 검색결과는 점수(_source)값을 기준으로 정렬된다.
- RDB의 order by 와 같은 역할 인듯
- 사용 방법:
- 오름 차순(default) : sort=필드명:asc (:asc는 생략 가능)
- 내림 차순 : sort=필드명:desc
실습5) books 인덱스에서 author필드가 jules인 도큐먼트를 pages 필드를 기준으로 오름차순 정렬과 내림차순 정렬로 검색해보자.
- 오름차순
pages가 189, 212, 304 로 쪽수가 적은 book부터 검색된다.
curl 'http://localhost:9200/books/_search?q=author:jules&sort=pages&pretty'
{
"took" : 25,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "VmvKC3cBf7K4q_TS667E",
"_score" : null,
"_source" : {
"title" : "Around the World in Eighty Days",
"author" : "Jules Verne",
"category" : "adventure novel",
"written" : "1873-07-01T10:30:00",
"pages" : 189,
"sell" : 27200000,
"plot" : "Fogg and Passepartout reach Suez in time. While disembarking in Egypt, they are watched by a Scotland Yard detective named Fix, who has been dispatched from London in search of a bank robber. Because Fogg matches the description of the robber, Fix mistakes Fogg for the criminal. Since he cannot secure a warrant in time, Fix boards the steamer conveying the travellers to Bombay. Fix becomes acquainted with Passepartout without revealing his purpose. Fogg promises the steamer engineer a large reward if he gets them to Bombay early. They dock two days ahead of schedule."
},
"sort" : [
189
]
},
{
"_index" : "books",
"_type" : "book",
"_id" : "VWvKC3cBf7K4q_TS667E",
"_score" : null,
"_source" : {
"title" : "Journey to the Center of the Earth",
"author" : "Jules Verne",
"category" : [
"Science fiction",
"adventure novel"
],
"written" : "1864-07-01T11:30:00",
"pages" : 212,
"sell" : 42100000,
"plot" : "Professor Lidenbrock decides to lock everyone in the house and force himself and the others (Axel, and the maid, Martha) to go without food until he cracks the code. Axel discovers the answer when fanning himself with the deciphered text: Lidenbrock's decipherment was correct, and only needs to be read backwards to reveal sentences written in rough Latin."
},
"sort" : [
212
]
},
{
"_index" : "books",
"_type" : "book",
"_id" : "VGvKC3cBf7K4q_TS667E",
"_score" : null,
"_source" : {
"title" : "Twenty Thousand Leagues Under the Sea",
"author" : "Jules Verne",
"category" : [
"Science fiction",
"adventure novel"
],
"written" : "1870-06-01T10:34:00",
"pages" : 304,
"sell" : 78100000,
"plot" : "In the year 1866, ships of several nations spot a mysterious sea monster, which some suggest to be a giant narwhal. The United States government assembles an expedition in New York City to find and destroy the monster. Professor Pierre Aronnax, a French marine biologist and narrator of the story, who happens to be in New York at the time, receives a last-minute invitation to join the expedition which he accepts. Canadian master harpoonist Ned Land and Aronnax's faithful servant Conseil are also brought aboard."
},
"sort" : [
304
]
}
]
}
}
- 내림차순
반대로 304, 212,189 로 페이지수가 많은 book부터 검색된다.
curl 'http://localhost:9200/books/_search?q=author:jules&sort=pages:desc&pretty'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "VGvKC3cBf7K4q_TS667E",
"_score" : null,
"_source" : {
"title" : "Twenty Thousand Leagues Under the Sea",
"author" : "Jules Verne",
"category" : [
"Science fiction",
"adventure novel"
],
"written" : "1870-06-01T10:34:00",
"pages" : 304,
"sell" : 78100000,
"plot" : "In the year 1866, ships of several nations spot a mysterious sea monster, which some suggest to be a giant narwhal. The United States government assembles an expedition in New York City to find and destroy the monster. Professor Pierre Aronnax, a French marine biologist and narrator of the story, who happens to be in New York at the time, receives a last-minute invitation to join the expedition which he accepts. Canadian master harpoonist Ned Land and Aronnax's faithful servant Conseil are also brought aboard."
},
"sort" : [
304
]
},
{
"_index" : "books",
"_type" : "book",
"_id" : "VWvKC3cBf7K4q_TS667E",
"_score" : null,
"_source" : {
"title" : "Journey to the Center of the Earth",
"author" : "Jules Verne",
"category" : [
"Science fiction",
"adventure novel"
],
"written" : "1864-07-01T11:30:00",
"pages" : 212,
"sell" : 42100000,
"plot" : "Professor Lidenbrock decides to lock everyone in the house and force himself and the others (Axel, and the maid, Martha) to go without food until he cracks the code. Axel discovers the answer when fanning himself with the deciphered text: Lidenbrock's decipherment was correct, and only needs to be read backwards to reveal sentences written in rough Latin."
},
"sort" : [
212
]
},
{
"_index" : "books",
"_type" : "book",
"_id" : "VmvKC3cBf7K4q_TS667E",
"_score" : null,
"_source" : {
"title" : "Around the World in Eighty Days",
"author" : "Jules Verne",
"category" : "adventure novel",
"written" : "1873-07-01T10:30:00",
"pages" : 189,
"sell" : 27200000,
"plot" : "Fogg and Passepartout reach Suez in time. While disembarking in Egypt, they are watched by a Scotland Yard detective named Fix, who has been dispatched from London in search of a bank robber. Because Fogg matches the description of the robber, Fix mistakes Fogg for the criminal. Since he cannot secure a warrant in time, Fix boards the steamer conveying the travellers to Bombay. Fix becomes acquainted with Passepartout without revealing his purpose. Fogg promises the steamer engineer a large reward if he gets them to Bombay early. They dock two days ahead of schedule."
},
"sort" : [
189
]
}
]
}
}
1.2.9. from
- 검색 결과를 몇 번째 값 부터 출력할지 지정
- 지정하지 않으면 기본값은 0이다.
- 스프링의 pageable 을 생각하면 이해하기 편할 것이다.
- 사용 방법:
- n번째 결과 부터 확인 : from=n-1
실습6) books 인덱스와 magazines 인게스에서 검색어 time으로 동시에 검색을 수행한 결과를 6번째 결과 부터 확인해보자.
찾은 data의 개수는 6개 이지만 from=5로 지정을 하였기 때문에 표시되는 결과는 1개이다.
curl 'http://localhost:9200/books,magazines/_search?q=time&from=5&pretty'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 2.5739596,
"hits" : [
{
"_index" : "magazines",
"_type" : "magazine",
"_id" : "C8Gr8IEBFUzGagAACXJP",
"_score" : 0.6931471,
"_source" : {
"title" : "People",
"company" : "Time Inc.",
"category" : "Human interest",
"issue" : "2013-10-01T00:00:00"
}
}
]
}
}
1.3 리퀘스트 바디 검색
- 검색할 조건을 JSON 데이터 형식의 질의로 입력해서 사용할 수 있다.
- URI 보다 더 복잡한 형식으로 검색
- QueryDSL을 사용한다.
1.3.1. term
- 가장 기본적인 텀(term) 쿼리를 사용하여 검색을 수행
실습7) books 인덱스와 author가 william인 값을 검색해보자.
curl 'localhost:9200/books/_search?pretty' --header 'content-type:application/json' -d '{"query":{"term":{"author":"william"}}}'
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 0.92442226,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "8cGq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"title" : "The Tempest",
"author" : "William Shakespeare",
"category" : "Comedies",
"written" : "1610-03-01T11:34:00",
"pages" : 62,
"sell" : 275600000,
"plot" : "Magician Prospero, rightful Duke of Milan, and his daughter, Miranda, have been stranded for twelve years on an island after Prospero's jealous brother Antonio (aided by Alonso, the King of Naples) deposed him and set him adrift with the then-3-year-old Miranda. Gonzalo, the King's counsellor, had secretly supplied their boat with plenty of food, water, clothes and the most-prized books from Prospero's library. Possessing magic powers due to his great learning, Prospero is reluctantly served by a spirit, Ariel, whom Prospero had rescued from a tree in which he had been trapped by the witch Sycorax. Prospero maintains Ariel's loyalty by repeatedly promising to release the \"airy spirit\" from servitude. Sycorax had been banished to the island, and had died before Prospero's arrival. Her son, Caliban, a deformed monster and the only non-spiritual inhabitant before the arrival of Prospero, was initially adopted and raised by him. He taught Prospero how to survive on the island, while Prospero and Miranda taught Caliban religion and their own language. Following Caliban's attempted rape of Miranda, he had been compelled by Prospero to serve as the magician's slave. In slavery, Caliban has come to view Prospero as a usurper and has grown to resent him and his daughter. Prospero and Miranda in turn view Caliban with contempt and disgust."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "8sGq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"title" : "The Merchant of Venice",
"author" : "William Shakespeare",
"category" : "Comedies",
"written" : "1596-02-01T04:42:00",
"pages" : 97,
"sell" : 124100000,
"plot" : "Bassanio, a young Venetian of noble rank, wishes to woo the beautiful and wealthy heiress Portia of Belmont. Having squandered his estate, he needs 3,000 ducats to subsidise his expenditures as a suitor. Bassanio approaches his friend Antonio, a wealthy merchant of Venice who has previously and repeatedly bailed him out. Antonio agrees, but since he is cash-poor – his ships and merchandise are busy at sea – he promises to cover a bond if Bassanio can find a lender, so Bassanio turns to the Jewish moneylender Shylock and names Antonio as the loan's guarantor."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "88Gq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"title" : "Romeo and Juliet",
"author" : "William Shakespeare",
"category" : "Tragedies",
"written" : "1562-12-01T20:40:00",
"pages" : 125,
"sell" : 182700000,
"plot" : "Meanwhile, Benvolio talks with his cousin Romeo, Montague's son, about Romeo's recent depression. Benvolio discovers that it stems from unrequited infatuation for a girl named Rosaline, one of Capulet's nieces. Persuaded by Benvolio and Mercutio, Romeo attends the ball at the Capulet house in hopes of meeting Rosaline. However, Romeo instead meets and falls in love with Juliet. Juliet's cousin, Tybalt, is enraged at Romeo for sneaking into the ball, but is only stopped from killing Romeo by Juliet's father, who doesn't wish to shed blood in his house. After the ball, in what is now called the \"balcony scene\", Romeo sneaks into the Capulet orchard and overhears Juliet at her window vowing her love to him in spite of her family's hatred of the Montagues. Romeo makes himself known to her and they agree to be married. With the help of Friar Laurence, who hopes to reconcile the two families through their children's union, they are secretly married the next day."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "9MGq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"title" : "King Lear",
"author" : "William Shakespeare",
"category" : "Tragedies",
"written" : "1603-05-01T04:36:00",
"pages" : 88,
"sell" : 91300000,
"plot" : "In the first scene the Earl of Gloucester and the Earl of Kent meet and observe that King Lear has awarded equal shares of his realm to the Duke of Cornwall and the Duke of Albany (and even before this the formal division of the next scene has taken place). Then the Earl of Gloucester introduces his illegitimate son Edmund to the Earl of Kent. In the next scene, King Lear, who is elderly and wants to retire from power, decides to divide his realm among his three daughters, and declares he'll offer the largest share to the one who loves him best. The eldest, Goneril, speaks first, declaring her love for her father in fulsome terms. Moved by her flattery Lear proceeds to grant to Goneril her share as soon as she's finished her declaration, before Regan and Cordelia have a chance to speak. He then awards to Regan her share as soon as she has spoken. When it is finally the turn of his youngest daughter, Cordelia, at first she refuses to say anything (\"Nothing, my Lord\") and then declares there is nothing to compare her love to, nor words to properly express it; she speaks honestly but bluntly, which infuriates him. In his anger he disinherits Cordelia and divides her share between Regan and Goneril. Kent objects to this unfair treatment. Enraged by Kent's protests, Lear banishes him from the country. Lear summons the Duke of Burgundy and the King of France, who have both proposed marriage to Cordelia. Learning that Cordelia has been disinherited, the Duke of Burgundy withdraws his suit, but the King of France is impressed by her honesty and marries her anyway."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "9cGq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"title" : "Hamlet",
"author" : "William Shakespeare",
"category" : "Tragedies",
"written" : "1599-06-01T12:34:00",
"pages" : 172,
"sell" : 146100000,
"plot" : "The protagonist of Hamlet is Prince Hamlet of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father's brother and successor. Claudius hastily married King Hamlet's widow, Gertrude, Hamlet's mother. Denmark has a long-standing feud with neighbouring Norway, and an invasion led by the Norwegian prince, Fortinbras, is expected."
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "9sGq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"title" : "Othello",
"author" : "William Shakespeare",
"category" : "Tragedies",
"written" : "1603-07-01T13:34:00",
"pages" : 100,
"sell" : 141200000,
"plot" : "Before Brabantio reaches Othello, news arrives in Venice that the Turks are going to attack Cyprus; therefore Othello is summoned to advise the senators. Brabantio arrives and accuses Othello of seducing Desdemona by witchcraft, but Othello defends himself successfully before an assembly that includes the Duke of Venice, Brabantio's kinsmen Lodovico and Gratiano, and various senators. He explains that Desdemona became enamored of him for the sad and compelling stories he told of his life before Venice, not because of any witchcraft. The senate is satisfied, but Brabantio leaves saying that Desdemona will betray Othello. By order of the Duke, Othello leaves Venice to command the Venetian armies against invading Turks on the island of Cyprus, accompanied by his new wife, his new lieutenant Cassio, his ensign Iago, and Iago's wife, Emilia as Desdemona's attendant."
}
}
]
}
}
1.3.1. size, from, fields
- 가장 기본적인 텀(term) 쿼리를 사용하여 검색을 수행
실습7) books 인덱스와 author가 william인 값을 검색하고 나온 결과를 2번째 결과부터 2개만 확인하되, 결과는 author과 title 필드에 대해서만 확인할 수 있도록 하자.
curl 'localhost:9200/books/_search?pretty' --header 'content-type:application/json' -d '{"from":1,"size":2,"_source":["title","author"], "query":{"term":{"author":"william"}}}'
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 0.92442226,
"hits" : [
{
"_index" : "books",
"_type" : "book",
"_id" : "8sGq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"author" : "William Shakespeare",
"title" : "The Merchant of Venice"
}
},
{
"_index" : "books",
"_type" : "book",
"_id" : "88Gq8IEBFUzGagAAtXEk",
"_score" : 0.92442226,
"_source" : {
"author" : "William Shakespeare",
"title" : "Romeo and Juliet"
}
}
]
}
}
'공부방 > Elasticsearch' 카테고리의 다른 글
elastic search manual (0) | 2022.08.22 |
---|---|
elasticsearch - 질의(QueryDSL) (0) | 2022.07.13 |
elasticsearch - bucket (0) | 2022.07.06 |
elasticsearch - metric aggregation (0) | 2022.07.06 |
elasticsearch - search (0) | 2022.07.06 |