도서 요약 / / 2022. 12. 22. 20:24

12. Improving Search Results

complete guide to elasticsearch를 정리한 자료입니다.
https://www.udemy.com/course/elasticsearch-complete-guide/

132. Proximity searches

PUT proximity/_doc/1
{
  "title": "Spicy Sauce"
}
PUT proximity/_doc/2
{
  "title": "Spicy Tomato Sauce"
}
PUT proximity/_doc/3
{
  "title": "Spicy Tomato and Garlic Sauce"
}
PUT proximity/_doc/4
{
  "title": "Tomato Sauce (spicy)"
}
PUT proximity/_doc/5
{
  "title": "Spicy and very delicious Tomato Sauce"
}
GET proximity/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "spicy sauce"
      }
    }
  }
}
GET proximity/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "spicy sauce",
        "slop": 1
      }
    }
  }
}

133. relavance scoring with proximity

GET proximity/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match":{
            "title": {
              "query": "spicy sauce"
            }
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "title": {
              "query": "spicy sauce",
              "slop": 5
            }
          }
        }
      ]
    }
  }
}

134. Fuzzy match query (handling typo)

GET proximity/_search
{
  "query": {
    "match": {
      "title": {
        "query": "splcy",
        "fuzziness": "auto"
      }
    }
  }
}
GET proximity/_search
{
  "query": {
    "match": {
      "title": {
        "query": "splcy tomato",
        "operator": "and", 
        "fuzziness": 1
      }
    }
  }
}

Transpositions

AB -> BA

135. Fuzzy query

136. Adding synonyms

PUT /synonyms
{
  "settings": {
    "analysis": {
      "filter": {
        "synonym_test": {
          "type": "synonym",
          "synonyms": [
            "awful => terrible",
            "awesome => great, super",
            "elasticsearch, logstash, kibana => elk",
            "weird, strange"
          ]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonym_test"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}
POST synonyms/_analyze
{
  "analyzer": "my_analyzer",
  "text": "awesome"
}
POST synonyms/_analyze
{
  "analyzer": "my_analyzer",
  "text": "Elasticsearch"
}
POST synonyms/_analyze
{
  "analyzer": "my_analyzer",
  "text": "weird"
}
POST synonyms/_analyze
{
  "analyzer": "my_analyzer",
  "text": "Elasticsearch is awesome, butt can also weird sometimes."
}

POST synonyms/_doc
{
  "description": "Elasticsearch is awesome, butt can also weird sometimes."
}
GET synonyms/_search
{
  "query": {
    "match": {
      "description": "great"
    }
  }
}
GET synonyms/_search
{
  "query": {
    "match": {
      "description": "awesome"
    }
  }
}

137. Adding synonyms from file

PUT /synonyms
{
  "settings": {
    "analysis": {
      "filter": {
        "synonym_test": {
          "type": "synonym",
          "synonyms_path": "analysis/synonyms.txt"
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonym_test"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}

138. Highlighting matches in fields

GET /highlighting/_search
{
  "_source": false,
  "query": {
    "match": { "description": "Elasticsearch story" }
  },
  "highlight": {
    "pre_tags": [ "<strong>" ],
    "post_tags": [ "</strong>" ],
    "fields": {
      "description" : {}
    }
  }
}

139. Stemming

PUT /stemming_test
{
  "settings": {
    "analysis": {
      "filter": {
        "synonym_test": {
          "type": "synonym",
          "synonyms": [
            "firm => company",
            "love, enjoy"
          ]
        },
        "stemmer_test" : {
          "type" : "stemmer",
          "name" : "english"
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonym_test",
            "stemmer_test"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}
PUT /stemming_test/_doc/1
{
  "description": "I love working for my firm!"
}
GET /stemming_test/_search
{
  "query": {
    "match": {
      "description": "enjoy work"
    }
  }
}
GET /stemming_test/_search
{
  "query": {
    "match": {
      "description": "love working"
    }
  }
}
GET /stemming_test/_search
{
  "query": {
    "match": {
      "description": "enjoy work"
    }
  },
  "highlight": {
    "fields": {
      "description": {}
    }
  }
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유