zoukankan      html  css  js  c++  java
  • Elasticsearch-搜索推荐

    对于es中搜索推荐的功能的实现:

    1.使用match_phrase_prefix来实现search-time搜索推荐,原理根match_phrase类似,唯一的区别是把最后一个term作为前缀去搜索,同时可以配置slop来调整搜索条件,也可以限制返回结果的数量,但是这种推荐方法还是需要用最后一个前缀去扫描大量的索引,性能会很差,在真实环境一般不推荐使用,我们可以使用以下第二种方式来实现。

    GET /forum/article/_search
    {
      "query": {
        "match_phrase_prefix": {
          "content": {
            "query": "java t",
            "slop":2,
            "max_expansions": 10
          }
        }
      }
    }

    2.第一种方式为search-time的搜索推荐机制,现在我们实现另一种index-time的搜索机制 
    什么是ngram,对于quick,5种长度下的ngram

    ngram length=1,q u i c k 
    ngram length=2,qu ui ic ck 
    ngram length=3,qui uic ick 
    ngram length=4,quic uick 
    ngram length=5,quick

    什么是edge ngram,对于quick,anchor首字母后进行ngram


    qu 
    qui 
    quic 
    quick

    使用edge ngram将每个单词都进行进一步的分词切分,用切分后的ngram来实现前缀搜索推荐功能 
    搜索的时候,不用再根据一个前缀,然后扫描整个倒排索引了; 简单的拿前缀去倒排索引中匹配即可,如果匹配上了,那么就直接返回结果

    2、实验一下ngram

    PUT /my_index 

    “settings”: { 
    “analysis”: { 
    “filter”: { 
    “autocomplete_filter”: { 
    “type”: “edge_ngram”, 
    “min_gram”: 1, 
    “max_gram”: 20 

    }, 
    “analyzer”: { 
    “autocomplete”: { 
    “type”: “custom”, 
    “tokenizer”: “standard”, 
    “filter”: [ 
    “lowercase”, 
    “autocomplete_filter” 





    }

    GET /my_index/_analyze 

    “analyzer”: “autocomplete”, 
    “text”: “quick brown” 
    }

    PUT /my_index/_mapping/my_type 

    “properties”: { 
    “title”: { 
    “type”: “string”, 
    “analyzer”: “autocomplete”, 
    “search_analyzer”: “standard” 


    }

    GET /my_index/my_type/_search 

    “query”: { 
    “match_phrase”: { 
    “title”: “hello w” 


    }

    如果用match,只有hello的也会出来,全文检索,只是分数比较低 
    推荐使用match_phrase,要求每个term都有,而且position刚好靠着1位,符合我们的期望的

    官方文档链接

  • 相关阅读:
    HDU1398Square Coins(母函数)
    HDU2079选课时间(母函数)
    HDU1028Ignatius and the Princess III(母函数)
    POJ1061青蛙的约会(扩展欧几里得)
    POJ1189钉子和小球(DP)
    POJ1179Polygon(DP)
    POJ2001Shortest Prefixes(字典树)
    POJ1157 LITTLE SHOP OF FLOWERS
    POJ3630Phone List(字典树)
    亲和串(HDU2203)
  • 原文地址:https://www.cnblogs.com/a-du/p/9295091.html
Copyright © 2011-2022 走看看