elastic stack-실습

신범철·2021년 10월 26일
0

elasticsearch

목록 보기
1/2

머리말

이 글은 개인적인 엘라스틱 서치 공부 내용을 담아놓은 글입니다.

참고 문헌
Elastic 가이드북

실습 코드

GET _search
{
  "query": {
    "match_all": {}
  }
}

GET logstash_index/_search

PUT /books
{
  "settings": {
    "number_of_shards": 5
    , "number_of_replicas": 1
  }
}

PUT books/_settings
{
  "number_of_replicas": 1
}

GET books

DELETE books

//cat API를 활용하여 ip, name, node.role조회가능
GET _cat/nodes?v&h=ip,name,node.role

//cat API를 활용하여 인덱스 정보
GET _cat/indices?v&h=health,index,docs.count,pri,rep

//전체 cat 조회
GET _cat

//샤드 조회
GET _cat/shards/books

//클러스터 세팅정보 조회 persisent는 계속 살아있는거고 transient는 종료하면 없어짐
get _cluster/settings

//모니터링 종료 다시 킬수도있
put _cluster/settings
{
  "persistent" : {
    "xpack" : {
      "monitoring" : {
        "collection" : {
          "enabled" : null
        }
      }
    }
  }
}

//CRUD 해보기

PUT my_index/_doc/1
{
  "name":"BeomChul Shin",
  "message":"안녕하세요 ELastic"
}

GET my_index/_doc/1

DELETE my_index/_doc/1

PUT my_index/_doc/1
{
  "age":40
}

//update 는 post사용

POST my_index/_doc/
{
  "name":"BeomChul Shin",
  "message":"안녕하세요 ELastic"
}

GET my_index/_doc/ykBHvXwBCbvUMtlG8Ku6

POST my_index/_update/1
{
  "doc":{
    "age":40
  }
}

//bulk API
//index, create, update, delete가능

DELETE test

POST _bulk
{"index":{"_index":"test","_id":"1"}}
{"field":"value one"}
{"index":{"_index":"test","_id":"2"}}
{"field":"value two"}
{"create":{"_index":"test","_id":"3"}}
{"field":"value three"}
{"update":{"_index":"test","_id":"1"}}
{"doc":{"field":"value two"}}

GET test/_doc/3


//search API

GET test/_search?q=field:three AND field:value

GET my_index/_search
{
  "query": {
    "match": {
      "message": "안녕하세요"
    }
  }
}


//풀 텍스트 쿼리

DELETE my_index

POST my_index/_bulk
{"index":{"_id":1}}
{"message":"The quick brown fox"}
{"index":{"_id":2}}
{"message":"The quick brown fox jumps over the lazy dog"}
{"index":{"_id":3}}
{"message":"The quick brown fox jumps over the quick dog"}
{"index":{"_id":4}}
{"message":"Brown fox brown dog"}
{"index":{"_id":5}}
{"message":"Lazy jumping dog"}


//match는 띄어쓰기하면 or로 인식
GET my_index/_search
{
  "query": {
    "match": {
      "message": {
        "query": "quick dog",
        "operator": "and"
      }
    }
  }
}

//match_phrase : 띄어쓰기까지 검색해서 그 단어가 나온것을 탐색, 구문검색
GET my_index/_search
{
  "query": {
    "match_phrase": {
      "message": "lazy dog"
    }
  }
}

//slop를 사용하면 n개 만큼 사이에 단ㄴ어 허용
GET my_index/_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "lazy dog",
        "slop": 1
      }
    }
  }
}

GET my_index/_search
{
  "query": {
    "query_string": {
        "default_field": "message",
        "query": "(jumping AND lazy) OR \"quick dog\""
      }
    }
  }
}

//bool 쿼리

GET my_index/_search
{
  "query": {
    "bool":{
     "must": [
       {
         "match": {
           "message": "quick"
         }
       },
       {
        "match_phrase": {
          "message": "lazy dog"
        }
       }
     ]
    }
  }
}

GET my_index/_search
{
  "query": {
    "bool":{
     "must": [
       {
         "match": {
           "message": "quick"
         }
       }
     ],
     "must_not": [
       {
        "match_phrase": {
          "message": "lazy dog"
        }
       }
     ]
    }
  }
}

//1,4,2순
GET my_index/_search
{
  "query": {
    "match": {
      "message": "fox"
    }
  }
}

GET my_index/_search
{
  "query": {
    "bool":{
      "must": [
        {
          "match": {
            "message": "fox"
          }
        }
      ],
      "should": [
        {
          "match": {
            "message": "lazy"
          }
        }
      ]
    }
  }
}

//범위 쿼리
POST phones/_bulk
{"index":{"_id":1}}
{"model":"Samsung GalaxyS 5","price":475,"date":"2014-02-24"}
{"index":{"_id":2}}
{"model":"Samsung GalaxyS 6","price":795,"date":"2015-03-15"}
{"index":{"_id":3}}
{"model":"Samsung GalaxyS 7","price":859,"date":"2016-02-21"}
{"index":{"_id":4}}
{"model":"Samsung GalaxyS 8","price":959,"date":"2017-03-29"}
{"index":{"_id":5}}
{"model":"Samsung GalaxyS 9","price":1059,"date":"2018-02-25"}

GET phones/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 700,
        "lt": 900
      }
    }
  }
}

GET phones/_search
{
  "query": {
    "range": {
      "date": {
        "gt": "2016-01-01",
        "lt": "2017-01-01"
      }
    }
  }
}


//데이터 색인과 텍스트 분석

//애널라이저
GET _analyze
{
  "text": "The quick brown fox jumps over the lazy dog",
  "tokenizer": "whitespace",
  "filter": [
    "lowercase",
    "stop",
    "snowball"
    ]
}


PUT my_index2
{
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "snowball"
      }
    }
  }
}


PUT my_index2/_doc/1
{
  "message" : "The quick brown fox jumps over the lazy dog"
}

GET my_index2/_search
{
  "query": {
    "match": {
      "message": "jump"
    }
  }
}

PUT my_index3
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer":{
        "my_custom_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "stop",
            "snowball"
            ]
        }
        }
        
      }
    }
  }
}

GET my_index3/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": ["The quick brown fox jumps over the lazy dog"]
}

DELETE my_index3


PUT my_index3
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer":{
        "my_custom_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "my_stop_filter",
            "snowball"
            ]
        }
        },
        "filter": {
          "my_stop_filter":{
            "type": "stop",
            "stopwords":[
              "brown"
              ]
          }
        }
        
      }
    }
  },
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "my_custom_analyzer"
      }
    }
  }
}


PUT my_index3/_doc/1
{
  "message" : "The quick brown fox jumps over the lazy dog"
}

GET my_index3/_search
{
  "query": {
    "match": {
      "message": "brown"
    }
  }
}

//텀벡터 역인덱스 내용을 확인하고 싶을때

GET my_index3/_termvectors/1?fields=message



//nori 한글 형태소 분석기
GET _analyze
{
  "analyzer": "nori",
  "text": ["동해물과 백두산이"]
}

PUT my_nori
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "my_nori_tokenizer": {
          "type": "nori_tokenizer",
          "user_dictionary_rules": [
            "해물"
            ]
        }
      }
    }
  }
}

GET my_nori/_analyze
{
  "tokenizer": "my_nori_tokenizer",
  "text": ["동해물과 백두산이"]
}

DELETE my_nori

PUT my_nori
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "nori_none": {
          "type": "nori_tokenizer",
          "decompound_mode": "none"
        },
        "nori_discard": {
          "type": "nori_tokenizer",
          "decompound_mode" : "discard"
          
        },
        "nori_mixed": {
          "type" : "nori_tokenizer",
          "decompound_mode": "mixed"
        }
      }
    }
  }
}

PUT my_pos
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "my_pos_f": {
            "type": "nori_part_of_speech",
            "stoptags": [
              "NR"
            ]
          }
        }
      }
    }
  }
}

GET my_pos/_analyze
{
  "tokenizer": "nori_tokenizer",
  "filter": [
    "my_pos_f"
  ],
  "text": "다섯아이가"
}



//매핑!
DELETE books

PUT books/_doc/1
{
  "title" : "Romeo and Juliet",
  "author" : "William Shakespeare",
  "category" : "Tragedies",
  "Publish_date": "1562-12-01T00:00:00",
  "pages" : 125
}

GET books/_mapping
//데이터가 입력되어 자동으로 매핑이 생성되기 전에 미리 먼저 인덱스의 매핑을 정의 해 놓으면 정의 해놓은 매핑에 맞추어 데이터가 입력됨

put books/_mapping
{
  "properties": {
    "content" : {
      "type": "text"
    }
  }
}

//키워드는 정확하게 다 입력해야함
GET books/_search
{
  "query": {
    "match": {
      "title.keyword": "Juliet"
    }
  }
}

put books
{
  "mappings": {
        "properties" : {
        "Publish_date" : {
          "type" : "date"
        },
        "author" : {
          "type" : "text",
          "fields" : {
            "full" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "category" : {
          "type" : "keyword"
        },
        "pages" : {
          "type" : "integer"
        },
        "title" : {
          "type" : "text"
        }
      }
}

}

GET books/_search
{
  "query": {
    "match": {
      "author.full": "William Shakespeare"
    }
  }
}


//숫자형
PUT number_test
{
  "mappings": {
    "properties": {
      "value": {
        "type": "integer"
      }
    }
  }
}


PUT number_test/_doc/1
{
  "value": "23"
}

PUT number_test/_doc/2
{
  "value": "2"
}
PUT number_test/_doc/3
{
  "value": 3.5
}

//integer형은 정수형이라 3.5가 찾아짐
GET number_test/_search
{
  "query": {
    "range": {
      "value": {
        "gt": 0.5,
        "lt": 3.1
      }
    }
  }
}


//인덱스 매핑과 데이터 타입 날짜, 객체, 위치정보
PUT my_date/_doc/1
{
  "date": "2017-02-03T17:12:20"
}

GET my_date/_mapping

DELETE my_date

put my_date
{
  "mappings": {
    "properties": {
      "date" : {
        "type": "date",
        "format" : "dd/MMM/yyyy:HH:mm:ss||iso8601||epoch_millis"
        
      }
    }
  }
}

PUT my_date/_doc/2
{
  "date": 1568332800000
}

GET my_date/_search

GET my_date/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "2015-06-01",
        "lte": "2020-08-01"
      }
    }
  }
}


//object

PUT movie/_doc/1
{
  "characters":{
    "name": "Iron Man",
    "age": 46,
    "side" : "superhero"
  }
}

GET movie/_mapping

GET movie/_search
{
  "query": {
    "match": {
      "characters.name": "Man"
    }
  }
}


PUT movie/_doc/2
{
  "title": "The Avengers",
  "characters": [
    {
      "name": "Iron Man",
      "side": "superhero"
    },
    {
      "name": "Loki",
      "side": "villain"
    }
  ]
}

PUT movie/_doc/3
{
  "title": "Avengers: Infinity War",
  "characters": [
    {
      "name": "Loki",
      "side": "superhero"
    },
    {
      "name": "Thanos",
      "side": "villain"
    }
  ]
}

GET movie/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "characters.name": "Loki"
          }
        }
      ]
    }
  }
}

DELETE movie

PUT movie
{
  "mappings": {
    "properties": {
      "characters": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          },
          "side": {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

//nested로 만들면 배열안에 객체가 각각의 독립적인 도큐먼트로 관리됨
GET movie/_search
{
  "query": {
    "nested": {
      "path": "characters",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "characters.name": "Loki"
              }
            },
            {
              "match": {
                "characters.side": "villain"
              }
            }
          ]
        }
      }
    }
  }
}


//Geo 위치 정보



PUT my_locations/_doc/1
{
  "location": {
    "lat": 41.12,
    "lon": -71.34
  }
}

GET my_locations/_mapping

PUT my_geo
{
  "mappings": {
    "properties": {
      "location": {
        "type":"geo_point"
      }
    }
  }
}

PUT my_geo/_doc/1
{
  "location": {
    "lat": 41.12,
    "lon": -71.34
  }
}

GET my_geo/_mapping




























profile
https://github.com/beombu

0개의 댓글