Elastic Search

[Elastic Search] MBTI 검색 ν”„λ‘œμ νŠΈ - 1. 검색 Score νŠœλ‹

Tempo 2022. 4. 12. 22:14

ν˜„μž¬ μ—˜λΌμŠ€ν‹±μ„œμΉ˜λ₯Ό μ΄μš©ν•΄ μˆ˜μ§‘ν•œ 데이터(MBTI νƒ€μž…λ³„ ν…μŠ€νŠΈ 데이터)λ₯Ό μ‘°νšŒν•˜λŠ” ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œ MBTI νƒ€μž…κ³Ό 슀마트폰(아이폰 λ˜λŠ” κ°€λŸ­μ‹œ)의 상관성을 λΆ„μ„ν•˜κΈ° μœ„ν•΄ ES의 쿼리λ₯Ό νŠœλ‹ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

 

인덱슀 ꡬ성

μ½˜ν…μΈ  λ‚΄λΆ€μ—μ„œ λͺ…μ‚¬λ§Œ μΆ”μΆœν•˜μ—¬ λΆ„μ„ν•˜κΈ° μœ„ν•΄ nori_nounμ΄λΌλŠ” 뢄석기λ₯Ό λ³„λ„λ‘œ μƒμ„±ν•˜μ—¬ ν•„λ“œλ‘œ μ„€μ •ν–ˆμŠ΅λ‹ˆλ‹€.

{
  "mbti" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "comment_cnt" : {
          "type" : "integer"
        },
        "contents" : {
          "type" : "text",
          "fields" : {
            "full" : {
              "type" : "keyword"
            },
            "nori_mixed" : {
              "type" : "text",
              "analyzer" : "nori_mixed",
              "search_analyzer" : "standard"
            },
            "nori_noun" : {
              "type" : "text",
              "analyzer" : "nori_pos_noun",
              "search_analyzer" : "standard"
            }
          }
        },
        "doc_url" : {
          "type" : "text"
        },
        "keyword" : {
          "type" : "keyword"
        },
        "like_cnt" : {
          "type" : "integer"
        },
        "platform" : {
          "type" : "keyword"
        },
        "published_at" : {
          "type" : "date"
        },
        "title" : {
          "type" : "text"
        },
        "writer" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "mbti",
        "creation_date" : "1649761141991",
        "analysis" : {
          "filter" : {
            "pos_filter" : {
              "type" : "nori_part_of_speech",
              "stoptags" : [
                "VV",
                "VA",
                "VX",
                "VCP",
                "VCN",
                "MM",
                "MAG",
                "MAJ",
                "IC",
                "J",
                "E",
                "XPN",
                "XSA",
                "XSN",
                "XSV",
                "SP",
                "SSC",
                "SSO",
                "SC",
                "SE",
                "UNA"
              ]
            }
          },
          "analyzer" : {
            "nori_mixed" : {
              "filter" : "shingle",
              "tokenizer" : "nori_t_mixed"
            },
            "nori_pos_noun" : {
              "filter" : "pos_filter",
              "type" : "custom",
              "tokenizer" : "nori_t_mixed"
            }
          },
          "tokenizer" : {
            "nori_t_mixed" : {
              "type" : "nori_tokenizer",
              "decompound_mode" : "mixed"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "e8w9oHlLSyqF5oDzviz0KA",
        "version" : {
          "created" : "7170299"
        }
      }
    }
  }
}

각 MBTI νƒ€μž…λ³„ 슀마트폰 μ„ ν˜Έλ„

GET mbti/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "contents.nori_noun": {
              "query": "ENFP"
            }
          }
        },
        {
          "match_phrase": {
            "contents": {
              "query": "아이폰",
              "boost": 2
            }
          }
        }
        ]
    }
  }
}

Match 쿼리에 μ½˜ν…μΈ  내뢀에 MBTI νƒ€μž…μ΄ μžˆλŠ”μ§€μ— λŒ€ν•΄ μš°μ„  μ‘°νšŒν•œλ‹€. 그리고 match_phrase둜 아이폰에 boostλ₯Ό ν•˜μ—¬ 아이폰이 ν¬ν•¨λœ 원문이 μƒμœ„ κ²€μƒ‰λ˜λ„λ‘ 쿼리λ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

사싀 match_phrase λŠ” ꡬ문, 즉 “아이폰 κΏ€νŒ”처럼 ꡬ문의 λ§€μΉ­ 정도λ₯Ό boost ν•΄μ£ΌκΈ° λ•Œλ¬Έμ— μœ„ μ‚¬λ‘€μ—μ„œλŠ” μ μ ˆν•œ μ‚¬μš© μ‚¬λ‘€λŠ” μ•„λ‹™λ‹ˆλ‹€. μ›λž˜λŠ” “ENFP 아이폰”으둜 κ²€μƒ‰ν•˜μ—¬ 각 단어 κ°„μ˜ Distanceλ₯Ό 검색 Score μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ μ‚¬μš©ν•˜λ €κ³  ν–ˆμ§€λ§Œ μ‹€νŒ¨ν•˜μ—¬ μš°μ„  match_phraseλ₯Ό μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

 

해결방법은? (μ•„λž˜λΆ€ν„°λŠ” μ‚½μ§ˆμ˜ μ—­μ‚¬μž…λ‹ˆλ‹€...)

#1 Should λ₯Ό μ‚¬μš©ν•œ 쑰회

Should의 경우 ν”νžˆ λ§ν•˜λŠ” or 쑰건으둜 쑰건에 ν¬ν•¨λœ 단어가 μƒμœ„λ‘œ μ˜¬λΌμ˜€λ„λ‘(즉 score μ μˆ˜κ°€ 더 λ†’μ•„μ§ˆ 수 μžˆλ„λ‘) 쿼리 λ³€κ²½

GET mbti/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "contents.nori_noun": {
              "query": "ENFP"
            }
          }
        }
        ],
      "should": [
        {
          "match": {
            "contents": {
              "query": "아이폰",
              "boost": 2
            }
          }
        },
        {
          "match": {
            "contents": {
              "query": "ENFP",
              "boost": 1
            }
          }
        }
        ]
    }
  }
}
λ°˜μ‘ν˜•