ElasticSearch 搜索推荐

发布于:2024-03-11 ⋅ 阅读:(53) ⋅ 点赞:(0)

Term Suggester

"suggest_mode":"missing"  missing 默认选项,不返回精准匹配到的分词结果
"suggest_mode":"popular"  popular 大于等于搜索词频率的返回
"suggest_mode":"always", 不做任何限制
 "min_doc_freq":2		出现在文档中的次数大于min_doc_freq的才返回,同一文档中多次出现算一次。
 GET /product/_search
{
  "suggest": {
    "desc_suggest": {
      "text":"手机中的",
      "term":{
        "field":"desc",
        "suggest_mode":"popular",
        "min_doc_freq":2
      }
    }
  }
}

Completion Suggester

基于内存而非索引(内存消耗大),性能好,只适合前缀搜索
结果精准度高召回率低

#添加新字段 设置suggest
PUT /product/_mapping
{
  "properties": {
    "desc_sugg": {
      "type": "text",
      "analyzer": "ik_max_word",
      "fields": {
        "suggest": {
          "type": "completion",
          "analyzer": "ik_max_word"
        }
      }
    }
  }
}

#给新字段赋值
POST /product/_update_by_query
{
  "script": {
    "source": "ctx._source.desc_sugg=ctx._source.desc",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}
# "skip_duplicates":true, 结果去重  
# "fuzziness":2 允许容错数
GET /product/_search
{
  "suggest": {
    "desc_suggest": {
      "prefix": "耳机",
      "completion": {
        "field": "desc_sugg.suggest",
        "skip_duplicates":true,
        "fuzzy":{
          "fuzziness":2
        }
      }
    }
  }
}

Context Suggester

新增索引
PUT product_suggest
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion",
        "contexts": [
          {
            "name": "place_type",
            "type": "category",
            "path": "tags"
          },
          {
            "name": "localtion",
            "type": "geo",
            "path": "loc",
            "precision": 4
          }
        ]
      },
      "loc":{
        "type": "geo_point"
      }
    }
  }
}
新增数据
PUT /product_suggest/_doc/1
{
  "suggest":["hello","word","tremhhh"],
  "tags":["no","hh"]
}

PUT /product_suggest/_doc/2
{
  "suggest":["hellhho","word","treeemhhh"],
  "tags":["monely"]
}

PUT /product_suggest/_doc/3
{
  "suggest": [
    "latlon",
    "word",
    "tremhhh"
  ],
  "loc": [
    {
      "lat": 43.56,
      "lon": -79.56
    },
    {
      "lat": 49.56,
      "lon": -59.56
    }
  ]
}
# "place_type":["no"] 在传入的指定分类中查询
# {"context":"monely","boost":2}  boost增加权重返回结果会排在前面
POST /product_suggest/_search
{
  "suggest": {
    "tag_suggest": {
      "prefix": "he",
      "completion": {
        "field": "suggest",
        "size":10,
        "skip_duplicates":true,
        "contexts":{
          "place_type":[
            {"context":"no"},
            {"context":"monely","boost":2}
            ]
        }
      }
    }
  }
}
#地理位置查询
POST /product_suggest/_search
{
  "suggest": {
    "tag_suggest": {
      "prefix": "tre",
      "completion": {
        "field": "suggest",
        "size": 10,
        "skip_duplicates": true,
        "contexts": {
          "localtion": [
            {
              "lat": 43.55,
              "lon": -79.55
            }
          ]
        }
      }
    }
  }
}

本文含有隐藏内容,请 开通VIP 后查看