Page tree
Skip to end of metadata
Go to start of metadata


사전 준비

  • week4_magazines.jsonweek4_books.json, week4_hotels.json 파일들을 _bulk API로 입력

    curl -XPOST -H 'Content-Type: application/json' localhost:9200/_bulk --data-binary @week4_books.json
    curl -XPOST -H 'Content-Type: application/json' localhost:9200/_bulk --data-binary @week4_magazines.json
    curl -XPOST -H 'Content-Type: application/json' localhost:9200/_bulk --data-binary @week4_hotels.json

단일 index, 단일 type 검색

GET /twitter/_search?q=kimchy  // "kimchy"를 검색
  • 결과값

    {
      "took" : 38,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 7,
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "twitter",
            "_type" : "_doc",
            "_id" : "M1hwcWkBUfgGJHUo0H0g",
            "_score" : 0.2876821,
            "_source" : {
              "post_date" : "2009-11-15T14:12:12",
              "foo" : "bar",
              "extra" : "test",
              "message" : "trying out Elasticsearch",
              "user" : "kimchy"
            }
          },
          ...
        ]
      }
    }
    
    
    

여러개의 index 검색

GET /books,magazines/_search?q=title:Three
  • 결과값

    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 10,
        "successful" : 10,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 0.7721133,
        "hits" : [
          {
            "_index" : "books",
            "_type" : "book",
            "_id" : "ZtSccWkBavgQMwrCZCkt",
            "_score" : 0.7721133,
            "_source" : {
              "title" : "The Three Musketeers",
              "author" : "Alexandre Dumas",
              "category" : "Historical novel",
              "written" : "1844-10-01T12:00:00",
              "pages" : 340,
              "sell" : 61400000,
              "plot" : "In 1625 France, d'Artagnan—a poor young nobleman—leaves his family in Gascony and travels to Paris with the intention of joining the Musketeers of the Guard. However, en route, at an inn in Meung-sur-Loire, an older man derides d'Artagnan's horse and, feeling insulted, d'Artagnan demands to fight a duel with him. The older man's companions beat d'Artagnan unconscious with a pot and a metal tong that breaks his sword. His letter of introduction to Monsieur de Tréville, the commander of the Musketeers, is stolen. D'Artagnan resolves to avenge himself upon the man, who is later revealed to be the Comte de Rochefort, an agent of Cardinal Richelieu, who is in Meung to pass orders from the Cardinal to Milady de Winter, another of his agents."
            }
          }
        ]
      }
    }

전체 index 검색

GET /_all/_search?q=The


GET /_search?q=The
  • 결과값은 동일하게 나옴


URI Search

  • 매개변수를 이용한 검색
GET /_search?q=title:time AND machine // "time"과 "machine"을 AND 조건으로 검색

parameter 로 사용 할 수 있는 매개변수 리스트

nameDescription

q

쿼리 문자열

df

필드 접두사가 쿼리 내에 정의되지 않은 경우 사용할 기본 필드

( filed:querystring 에서 filed가 지정되지 않았을 경우)

// GET /_search?q=time&df=title

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 49,
    "successful" : 48,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.4226693,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "Z9SccWkBavgQMwrCZCkt",
        "_score" : 1.4226693,
        "_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" : "sZf_bGkBkHJ9NOLT7aRe",
        "_score" : 0.9808292,
        "_source" : {
          "title" : "Time",
          "company" : "Time Inc.",
          "category" : "News magazine",
          "issue" : "2014-03-01T00:00:00"
        }
      },
      {
        "_index" : "magazines",
        "_type" : "magazine",
        "_id" : "rZf_bGkBkHJ9NOLTpKSN",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Time",
          "company" : "Time Inc.",
          "category" : "News magazine",
          "issue" : "2014-03-01T00:00:00"
        }
      },
      {
        "_index" : "magazines",
        "_type" : "magazine",
        "_id" : "bdSccWkBavgQMwrCkSmB",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Time",
          "company" : "Time Inc.",
          "category" : "News magazine",
          "issue" : "2014-03-01T00:00:00"
        }
      }
    ]
  }
}

analyzer *

쿼리 문자열을 분석 할 때 사용할 분석기 이름

analyze_wildcard *

와일드 카드와 접두사 쿼리를 분석에 사용할 것인가에 대한 설정, 기본값은 false

batched_reduce_size

조정 노드에서 한 번에 줄여야하는 샤드 결과 수. 이 값은 요청에서 샤드의 잠재적 인수가 커질 수있는 경우 검색 요청 당 메모리 오버 헤드를 줄이기 위한 보호 메커니즘으로 사용한다

default_operator

기본 연산자를 지정. AND또는 OR를 사용가능하다. default는 OR

lenient *

true로 설정하면 형식 기반 오류 (예 : 숫자 필드에 텍스트 제공)가 무시됩니다. 기본값은 false입니다.

explain *

검색 처리 결과 표시

_source

검색결과에 도큐먼트 내용을 받지 않음

_source_includes 설정으로 값을 받을 fileds를 설정할 수 있다

_source_excludes 설정으로 값을 안받을 fileds를 설정할 수 있다

stored_fields *

hits 리스트에서 받을 도큐먼트의 filed 들을 설정

설정하지 않으면 _source 랑 동일한 response가 돌아온다

sort

정렬할 순서를 지정

fieldName 또는 fieldName:ascfieldName:desc의 형태로 지정할 수 있다.있습니다. 

track_scores

정렬시 true점수를 추적하고 그 값을 반환하도록 설정

track_total_hits *

로 설정 false쿼리와 일치하는 히트의 총 수의 추적을 해제하기 위해. ( 자세한 내용 은 색인 정렬 참조). default는 true

timeout

지정된 시간 값 내에서 실행될 검색 요청의 경계를 지정하고 만료되었을 때 해당 지점까지 누적 된 히트 수를 표시하는 검색 제한 시간. default는 시간 제한 없음

terminate_after

각 쿼리에 대해 수집 할 최대 문서 수. 쿼리 실행이 일찍 끝나는 시점에 도달합니다. 설정되면 응답 terminated_early에는 쿼리 실행이 실제로 종결되었는지 여부를 나타내는 부울 필드가 있습니다 . default는 no terminate_after

from

조회수의 인덱스에서 시작하여 반환됩니다. default는 0

size

hits 리스트의 limit 값. default는 10

// GET /books/_search?size=1&q=author:William&_source=false

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.9092852,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YdSccWkBavgQMwrCZCkt",
        "_score" : 0.9092852
      }
    ]
  }
}

search_type

검색 작업의 유형설정 (dfs_query_then_fetch, query_then_fetch(default))

// GET /books/_search?q=author:William&search_type=query_then_fetch&_source=false
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.9092852,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YdSccWkBavgQMwrCZCkt",
        "_score" : 0.9092852
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YtSccWkBavgQMwrCZCkt",
        "_score" : 0.9092852
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "XtSccWkBavgQMwrCZCkt",
        "_score" : 0.49917623
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YNSccWkBavgQMwrCZCkt",
        "_score" : 0.49917623
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "XdSccWkBavgQMwrCZCks",
        "_score" : 0.47000363
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "X9SccWkBavgQMwrCZCkt",
        "_score" : 0.47000363
      }
    ]
  }
}


// GET /books/_search?q=author:William&search_type=dfs_query_then_fetch&_source=false
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.9244223,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "XdSccWkBavgQMwrCZCks",
        "_score" : 0.9244223
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "X9SccWkBavgQMwrCZCkt",
        "_score" : 0.9244223
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YdSccWkBavgQMwrCZCkt",
        "_score" : 0.9244223
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YtSccWkBavgQMwrCZCkt",
        "_score" : 0.9244223
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "XtSccWkBavgQMwrCZCkt",
        "_score" : 0.9244223
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YNSccWkBavgQMwrCZCkt",
        "_score" : 0.9244223
      }
    ]
  }
}

allow_partial_search_results

false요청이 부분 결과를 생성하는 경우 전체 실패를 반환하도록 설정합니다 . 기본값은 true이며 시간 초과 또는 부분 실패의 경우 부분적인 결과가 허용됩니다. 이 기본값은 클러스터 수준 설정을 사용하여 제어 할 수 있습니다search.default_allow_partial_results



Reauest Body Search

  • 리퀘스트 바디로 search 옵션을 설정해서 검색하는 것

query

  • 리퀘스트 바디로 term 속성을 줘서 검색
GET /books/_search?pretty
{
  "query" : {
    "term" : { "author" : "william" }
  }

/*결과*/
{{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.9092852,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YdSccWkBavgQMwrCZCkt",
        "_score" : 0.9092852,
        "_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" : "YtSccWkBavgQMwrCZCkt",
        "_score" : 0.9092852,
        "_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."
        }
      },
      ...
    ]
  }
}

size, from, fields

GET /_search?pretty
{
  "from" : 1,
  "size" : 2,
  "_source": ["title","category"],
  "query" : {
    "term" : {  "author" : "william"  }
  }
}

/*결과*/
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 59,
    "successful" : 58,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.9092852,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "YtSccWkBavgQMwrCZCkt",
        "_score" : 0.9092852,
        "_source" : {
          "title" : "Othello",
          "category" : "Tragedies"
        }
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "XtSccWkBavgQMwrCZCkt",
        "_score" : 0.49917623,
        "_source" : {
          "title" : "The Merchant of Venice",
          "category" : "Comedies"
        }
      }
    ]
  }
}

sort

  • sort order : asc, desc
  • sort mode option
option

min

가장 작은 token 기준

max

가장 큰 token 기준

sum

token 합 기준, only number

avg

token 평균 기준, only number

median

token들을 정렬했을때 중간값 기준, only number


_source

{
	"_source": false, // 도큐먼트 내용 보이지 않게함
}


{
	"_source": ["field1", "field2", "field*"], // "field1", "field2" 와 같은 필드명인 필드와 "field"문자열로 시작되는 필드만 결과로 받음 
}


highlight

  • query 검색 term 을 강조한다
GET /books/_search
{
  "query" : {
    "term" : { "author" : "william" }
  },
  "highlight" : {
    "pre_tags" : ["<strong>"], //강조 태그 지정, 지정 안하면 <em></em>으로 강조됨
    "post_tags" : ["</strong>"],
    "fields" : { "author" : { } }
  },
  "size": 1,
  "_source": "author"
}


/*결과*/
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.261305,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "09SqcmkBavgQMwrCKCm0",
        "_score" : 1.261305,
        "_source" : {
          "author" : "William Shakespeare"
        },
        "highlight" : {
          "author" : [
            "<strong>William</strong> Shakespeare"
          ]
        }
      }
    ]
  }
}



Search Template

  • 검색 시 template을 지정해서 파라미터 형식으로 넘겨줄 수 있음
GET /_search/template
{
    "source" : {
      "query": { "match" : { "{{my_field}}" : "{{my_value}}" } },
      "size" : "{{my_size}}"
    },
    "params" : {
        "my_field" : "title",
        "my_value" : "the",
        "my_size" : 5
    }
}


/*결과*/
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 59,
    "successful" : 58,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 11,
    "max_score" : 0.8142733,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "1NSqcmkBavgQMwrCKCm0",
        "_score" : 0.8142733,
        "_source" : {
          "title" : "The Adventures of Tom Sawyer",
          "author" : "Mark Twain",
          "category" : "Folk",
          "written" : "1876-03-01T01:34:00",
          "pages" : 220,
          "sell" : 83200000,
          "plot" : """Tom Sawyer lives with his Aunt Polly and his half-brother, Sid. Tom dirties his clothes in a fight and is made to whitewash the fence the next day, as a punishment. He cleverly persuades his friends to trade him small treasures for the privilege of doing his work. He trades the treasures for Sunday School tickets which one normally receives for memorizing scriptures, redeeming them for a bible, much to the surprise and bewilderment of the superintendent who thought "it was simply preposterous that this boy had warehoused two thousand sheaves of Scriptural wisdom on his premises—a dozen would strain his capacity, without a doubt.""""
        }
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "19SqcmkBavgQMwrCKCm0",
        "_score" : 0.35667494,
        "_source" : {
          "title" : "The Three Musketeers",
          "author" : "Alexandre Dumas",
          "category" : "Historical novel",
          "written" : "1844-10-01T12:00:00",
          "pages" : 340,
          "sell" : 61400000,
          "plot" : "In 1625 France, d'Artagnan—a poor young nobleman—leaves his family in Gascony and travels to Paris with the intention of joining the Musketeers of the Guard. However, en route, at an inn in Meung-sur-Loire, an older man derides d'Artagnan's horse and, feeling insulted, d'Artagnan demands to fight a duel with him. The older man's companions beat d'Artagnan unconscious with a pot and a metal tong that breaks his sword. His letter of introduction to Monsieur de Tréville, the commander of the Musketeers, is stolen. D'Artagnan resolves to avenge himself upon the man, who is later revealed to be the Comte de Rochefort, an agent of Cardinal Richelieu, who is in Meung to pass orders from the Cardinal to Milady de Winter, another of his agents."
        }
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "2dSqcmkBavgQMwrCKCm0",
        "_score" : 0.35667494,
        "_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" : "books",
        "_type" : "book",
        "_id" : "29SqcmkBavgQMwrCKCm0",
        "_score" : 0.3503948,
        "_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."
        }
      },
      {
        "_index" : "books",
        "_type" : "book",
        "_id" : "z9SqcmkBavgQMwrCKCm0",
        "_score" : 0.30873197,
        "_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."
        }
      }
    ]
  }
}



Search Shards API

  • shards의 정보를 검색하기 위한 API

GET /twitter/_search_shards


/*결과*/
{
  "nodes" : {
    "AvZ4mIRYTvWXyVmL2gxvKg" : {
      "name" : "AvZ4mIR",
      "ephemeral_id" : "WmsE6_QmQUKIWIHN8tr8Og",
      "transport_address" : "127.0.0.1:9300",
      "attributes" : {
        "ml.machine_memory" : "17179869184",
        "xpack.installed" : "true",
        "ml.max_open_jobs" : "20",
        "ml.enabled" : "true"
      }
    }
  },
  "indices" : {
    "twitter" : { }
  },
  "shards" : [
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "AvZ4mIRYTvWXyVmL2gxvKg",
        "relocating_node" : null,
        "shard" : 0,
        "index" : "twitter",
        "allocation_id" : {
          "id" : "LX5roCiBSfuTzKUi68Dxfw"
        }
      }
    ],
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "AvZ4mIRYTvWXyVmL2gxvKg",
        "relocating_node" : null,
        "shard" : 1,
        "index" : "twitter",
        "allocation_id" : {
          "id" : "Y8yJ5th4SNSJs9x0Aw4faA"
        }
      }
    ],
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "AvZ4mIRYTvWXyVmL2gxvKg",
        "relocating_node" : null,
        "shard" : 2,
        "index" : "twitter",
        "allocation_id" : {
          "id" : "12ukbIKtSxiIK2jw4N7Kcg"
        }
      }
    ],
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "AvZ4mIRYTvWXyVmL2gxvKg",
        "relocating_node" : null,
        "shard" : 3,
        "index" : "twitter",
        "allocation_id" : {
          "id" : "feyi3bKHRZOtOone2gvmSg"
        }
      }
    ],
    [
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "AvZ4mIRYTvWXyVmL2gxvKg",
        "relocating_node" : null,
        "shard" : 4,
        "index" : "twitter",
        "allocation_id" : {
          "id" : "AmCtpfelTPiUzWluVsBLBg"
        }
      }
    ]
  ]
}



참고링크



  • No labels