ElasticSearch 搜索推荐

Term Suggester

java 复制代码
"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

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

#添加新字段 设置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

java 复制代码
新增索引
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
            }
          ]
        }
      }
    }
  }
}
相关推荐
Elastic 中国社区官方博客9 小时前
如何将数据从 AWS S3 导入到 Elastic Cloud - 第 3 部分:Elastic S3 连接器
大数据·elasticsearch·搜索引擎·云计算·全文检索·可用性测试·aws
掘金-我是哪吒9 小时前
微服务mysql,redis,elasticsearch, kibana,cassandra,mongodb, kafka
redis·mysql·mongodb·elasticsearch·微服务
研究是为了理解11 小时前
Git Bash 常用命令
git·elasticsearch·bash
晨欣14 小时前
Elasticsearch和Lucene之间是什么关系?(ChatGPT回答)
elasticsearch·chatgpt·lucene
筱源源20 小时前
Elasticsearch-linux环境部署
linux·elasticsearch
Elastic 中国社区官方博客1 天前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理
Shenqi Lotus2 天前
ELK-ELK基本概念_ElasticSearch的配置
elk·elasticsearch
yeye198912242 天前
10-Query & Filtering 与多字符串多字段查询
elasticsearch
Narutolxy2 天前
精准优化Elasticsearch:磁盘空间管理与性能提升技巧20241106
大数据·elasticsearch·jenkins
谢小涛3 天前
ES管理工具Cerebro 0.8.5 Windows版本安装及启动
elasticsearch·es·cerebro