220307 Elastic Search

25tutmmu·2022년 3월 7일
0

Elastic Search Guidebook

localhost:9200/index/type/id <= 요즘은 type대신 _doc을 씀
ver.8은 보안이 추가가 되어 username/password를 추가하여야함

상품 CRUD

Post: 만들기

localhost:9200/myproduct/_doc/1

{
    "name": "최신 마우스",
    "description": "안녕하세요. Bestshop입니다! 국내 최고 Best 상품만 판매합니다.",
    "price": 10000
}

Get: 조회

localhost:9200/myproduct/_doc/1

{
    "_index": "myproduct",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "최신 마우스",
        "description": "안녕하세요. Bestshop입니다! 국내 최고 Best 상품만 판매합니다.",
        "price": 10000
    }
}

post: 모든 게시글 검색

localhost:9200/myproduct/_search

{
    "took": 438,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "myproduct",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "최신 마우스",
                    "description": "안녕하세요. Bestshop입니다! 국내 최고 Best 상품만 판매합니다.",
                    "price": 10000
                }
            },
            {
                "_index": "myproduct",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "name": "기계식 키보드",
                    "description": "기계식 키보드 오늘만 특가",
                    "price": 20000
                }
            }
        ]
    }
}

PUT: 수정하기

localhost:9200/myproduct/_doc/2

{
          "name": "기계식 키보드(청축)",
          "description": "기계식 키보드 오늘만 특가",
          "price": 20000
}

DELETE: 삭제

localhost:9200/myproduct/_doc/2

POST: "Best"로 검색하기

body 안에 찾고싶은 단어 넣기


{
    "query": {
        "match": {
            "description" : "Best"
        }
    }
}

결과

{
    "took": 40,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.84290004,
        "hits": [
            {
                "_index": "myproduct",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.84290004,
                "_source": {
                    "name": "최신 마우스",
                    "description": "안녕하세요. Bestshop입니다! 국내 최고 Best 상품만 판매합니다.",
                    "price": 10000
                }
            }
        ]
    }
}

=> Best는 검색이 되지만 Best shop은 검색 되지 않는다.
띄어쓰기로 token화 되었기 때문에 'BestShop입니다.' 까지 검색하여야 검색된다.

Analyzer

localhost:9200/myproduct/_analyze

{
    "text": "안녕하세요. Bestshop입니다! Best"
}

결과

{
    "tokens": [
        {
            "token": "안녕하세요",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<HANGUL>",
            "position": 0
        },
        {
            "token": "bestshop입니다",
            "start_offset": 7,
            "end_offset": 18,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "best",
            "start_offset": 20,
            "end_offset": 24,
            "type": "<ALPHANUM>",
            "position": 2
        }
    ]
}

Analyze 종류

  • standard
  • whitespace
  • keyword
  • snowball

검색 결과의 경우 <= Standard(default analyzer)
1. 특수문자 없애기 Character-Filter
2. 띄어쓰기로 자르기 Tokenizer
<= tokenizer 의 경우 무조건 1개만 있어야함
3. 소문자로 바뀜 Token-Filter
Whitespace => 대소문자 구별함

{
    "analyzer": "whitespace",
    "text": "안녕하세요. Bestshop입니다! Best"
}

{
    "tokens": [
        {
            "token": "안녕하세요.",
            "start_offset": 0,
            "end_offset": 6,
            "type": "word",
            "position": 0
        },
        {
            "token": "Bestshop입니다!",
            "start_offset": 7,
            "end_offset": 19,
            "type": "word",
            "position": 1
        },
        {
            "token": "Best",
            "start_offset": 20,
            "end_offset": 24,
            "type": "word",
            "position": 2
        }
    ]
}

keyword => text의 전체가 하나의 keyword 화가 됨

{
    "analyzer": "keyword",
    "text": "안녕하세요. Bestshop입니다! Best"
}

{
    "tokens": [
        {
            "token": "안녕하세요. Bestshop입니다! Best",
            "start_offset": 0,
            "end_offset": 24,
            "type": "word",
            "position": 0
        }
    ]
}

snowball going로 go로 바꿔줌

{
    "analyzer": "snowball",
    "text": "안녕하세요. Bestshop입니다! Best going"
}

{
    "tokens": [
        {
            "token": "안녕하세요.",
            "start_offset": 0,
            "end_offset": 6,
            "type": "word",
            "position": 0
        },
        {
            "token": "Bestshop입니다!",
            "start_offset": 7,
            "end_offset": 19,
            "type": "word",
            "position": 1
        },
        {
            "token": "Best",
            "start_offset": 20,
            "end_offset": 24,
            "type": "word",
            "position": 2
        }
    ]
}

나만의 analyzer 만들기

post man PUT Body에 적기
localhost:9200/myproduct2

{
      "settings": {
        "analysis": {
          "analyzer": {
            "my_ngram_analyzer": {
              "tokenizer": "my_ngram_tokenizer"    
            }
          },
          "tokenizer": {
            "my_ngram_tokenizer": {
              "type": "nGram",
              "min_gram": "1",
              "max_gram": "10"
            }
          }
        },
        "max_ngram_diff" : "10"
      }
    }

localhost:9200/myproduct2/_settings 에서 만들 analyzer 확인하기

{
    "analyzer": "my_ngram_analyzer",
    "text": "안녕하세요. Bestshop입니다! Best "
}

결과

"tokens": [
        {
            "token": "안",
            "start_offset": 0,
            "end_offset": 1,
            "type": "word",
            "position": 0
        },
        {
            "token": "안녕",
            "start_offset": 0,
            "end_offset": 2,
            "type": "word",
            "position": 1
        },
        {
            "token": "안녕하",
            "start_offset": 0,
            "end_offset": 3,
            "type": "word",
            "position": 2
        },
        {
            "token": "안녕하세",
            "start_offset": 0,
            "end_offset": 4,
            "type": "word",
            "position": 3
        },
        {
            "token": "안녕하세요",
            "start_offset": 0,
            "end_offset": 5,
            "type": "word",
            "position": 4
        },
        {
            "token": "안녕하세요.",
            "start_offset": 0,
            "end_offset": 6,
            "type": "word",
            "position": 5
        },

이런식으로 나옴

mappings

localhost:9200/myproduct2/_mappings postman PUT

{
      "properties": {
        "name": {
          "type": "text"
        },
        "description": {
          "type": "text",
          "analyzer": "my_ngram_analyzer"
        },
        "price": {
          "type": "long"
        }
      }
    }
    
    
 반환 
 {
    "acknowledged": true
}

mapping의 경우 한번 등록 후에는 다시 변경 및 수정을 할 수 없음

자동완성

{
    "query": {
        "bool":{
            "should":[{
                "prefix": {
                    "name": "키"
                }
            }]
        }
    }
}

auto complete

0개의 댓글