elastic search 정리
여기꺼 정리
http://joelabrahamsson.com/elasticsearch-101/
* Document 입력
http://localhost:9200/<index>/<type>/[<id>]
PUT /movies/movie/1
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}
* ID로 가져오기
curl -XGET "http://localhost:9200/movies/movie/1" -d''
* ID로 삭제
curl -XDELETE "http://localhost:9200/movies/movie/1" -d''
* 검색 1 : 전체 검색
http://localhost:9200/<index>/<type>/_search
- curl -XGET "http://v6.cine21.com:9200/_search"
: 전체 목록 보기
- curl -XGET "http://v6.cine21.com:9200/movies/_search"
: movies index안의 모든 type들
- curl -XGET "http://v6.cine21.com:9200/movies/movie/_search"
: movies index안의 movie type
* 검색 2 : _search 엔드포인트 사용
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-dsl-query-string-query
- 기본형 :
curl -XPOST "http://localhost:9200/_search" -d' {
"query": {
//Query DSL here
}
}'
- 전체에서 검색 :
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "kill"
}
}
}'
- 특정필드에서 검색
curl -XPOST "http://v6.cine21.com:9200/_search" -d'
{
"query": {
"query_string": {
"query": "ford",
"fields": ["title"]
}
}
}'
* 검색 3 : 필터링
- 기본형 : query, filter 두가지로 구성
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "drama"
}
},
"filter": {
//Filter to apply to the query
}
}
}
}'
- team filter : http://www.elasticsearch.org/guide/reference/query-dsl/term-filter/
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "drama"
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}'
- 쿼리없이 필터링만 하기(전체에서 쿼리)
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}'
# 이렇게 줄여쓴다.
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"constant_score": {
"filter": {
"term": { "year": 1962 }
}
}
}
}'
* 맵핑
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"constant_score": {
"filter": {
"term": { "director": "Francis Ford Coppola" }
}
}
}
}'
* 안나온다. -_-; 데이터를 저장할때(인덱스할때) es는 두가지 일을 한다. (원본을 저장 / 각 오브젝트를 Lucene인덱스로 분해). 이때 매핑정보를 지정해주지 않으면 필드타입(문자, 숫자)에 따른 매핑만 하는것이 디폴트.
즉, director 필드의 값은 "Francis Ford Coppola"가 아니라 ["francis", "ford", "coppola"] 이런식임.
다음처럼 하면 나온다.
curl -XPOST "http://v6.cine21.com:9200/_search" -d'
{
"query": {
"constant_score": {
"filter": {
"term": { "director": "coppola" }
}
}
}
}'
- 매핑 방법 : _mapping 엔드포인트 사용
curl -XPUT "http://localhost:9200/movies/movie/_mapping" -d'
{
"movie": {
"properties": {
"director": {
"type": "string",
"index": "not_analyzed"
}
}
}
}'
* 실행하면 에러난다. -_-; 이유는..
1. 기존의 매핑이 존재하고(그러므로 인덱스 자체를 새로해야함)
2. 한가지 매핑밖에 없어서, 반대로 한 단어로(coppola 등) 검색이 안된다.
curl -XPUT "http://localhost:9200/movies/movie/_mapping" -d'
{
"movie": {
"properties": {
"director": {
"type": "multi_field",
"fields": {
"director": {"type": "string"},
"original": {"type" : "string", "index" : "not_analyzed"}
}
}
}
}
}'
두번 인덱싱 하자. 한번은 기본처럼 이름으로 찢어서, 한번은 director.original의 이름으로 원본을 저장하면서.(여기서는 매핑후 문서인덱싱을 다시 한번 해주자)