跳转到主要内容
POST
/
plugins
/
wk.plugin.search
/
usersearch
curl -X POST "http://localhost:5001/plugins/wk.plugin.search/usersearch" \
  -H "Content-Type: application/json" \
  -d '{
    "uid": "user123",
    "payload": {
      "content": "北京"
    },
    "payload_types": [1, 2],
    "channel_type": 2,
    "limit": 10,
    "page": 1,
    "highlights": ["payload.content"]
  }'
{
  "total": 25,
  "limit": 10,
  "page": 1,
  "messages": [
    {
      "message_id": 1234,
      "message_idstr": "1234",
      "message_seq": 1,
      "client_msg_no": "djzdfdfdf",
      "from_uid": "u1",
      "channel_id": "g1",
      "channel_type": 2,
      "payload": {
        "type": 1,
        "content": "你是<mark>北京</mark>大学的吗"
      },
      "topic": "",
      "timestamp": 762834
    },
    {
      "message_id": 1235,
      "message_idstr": "1235",
      "message_seq": 2,
      "client_msg_no": "djzdfdfde",
      "from_uid": "u2",
      "channel_id": "g1",
      "channel_type": 2,
      "payload": {
        "type": 1,
        "content": "我在<mark>北京</mark>工作"
      },
      "topic": "",
      "timestamp": 762835
    }
  ]
}

概述

搜索属于当前用户的所有消息,支持多维度搜索和中文分词功能。
  • 需要 WuKongIM v2.1.3-20250210 或以上版本
  • 需要安装 wk.plugin.search 插件
  • 插件使用文档:插件使用指南

请求体

uid
string
required
当前用户 UID(限制搜索指定用户的消息)
payload
object
消息 payload,支持搜索自定义字段
payload_types
array
消息类型搜索
payload_types[]
integer
消息类型值
from_uid
string
发送者 UID
channel_id
string
频道 ID,指定频道后只搜索此频道内的消息
channel_type
integer
频道类型
  • 1 - 个人频道
  • 2 - 群组频道
topic
string
根据 topic 搜索
limit
integer
查询限制数量,默认 10
page
integer
页码,分页使用,默认为 1
start_time
integer
消息时间(开始),Unix 时间戳
end_time
integer
消息时间(结束,结果包含 end_time),Unix 时间戳
highlights
array
需要高亮显示的关键字字段
highlights[]
string
字段名,如 “payload.content”
curl -X POST "http://localhost:5001/plugins/wk.plugin.search/usersearch" \
  -H "Content-Type: application/json" \
  -d '{
    "uid": "user123",
    "payload": {
      "content": "北京"
    },
    "payload_types": [1, 2],
    "channel_type": 2,
    "limit": 10,
    "page": 1,
    "highlights": ["payload.content"]
  }'
{
  "total": 25,
  "limit": 10,
  "page": 1,
  "messages": [
    {
      "message_id": 1234,
      "message_idstr": "1234",
      "message_seq": 1,
      "client_msg_no": "djzdfdfdf",
      "from_uid": "u1",
      "channel_id": "g1",
      "channel_type": 2,
      "payload": {
        "type": 1,
        "content": "你是<mark>北京</mark>大学的吗"
      },
      "topic": "",
      "timestamp": 762834
    },
    {
      "message_id": 1235,
      "message_idstr": "1235",
      "message_seq": 2,
      "client_msg_no": "djzdfdfde",
      "from_uid": "u2",
      "channel_id": "g1",
      "channel_type": 2,
      "payload": {
        "type": 1,
        "content": "我在<mark>北京</mark>工作"
      },
      "topic": "",
      "timestamp": 762835
    }
  ]
}

响应字段

total
integer
required
消息总数量
limit
integer
required
查询数量限制
page
integer
required
当前页码
messages
array
required
消息列表

状态码

状态码说明
200搜索成功
400请求参数错误
403没有搜索权限
500服务器内部错误

搜索功能特性

中文分词搜索

支持中文分词,能够智能识别中文词汇进行搜索。 示例
  • 搜索 “北京大学” 可以匹配包含 “北京” 或 “大学” 的消息
  • 支持模糊匹配和精确匹配

多维度搜索

支持多个维度的组合搜索:
  1. 内容搜索:通过 payload.content 搜索消息内容
  2. 类型搜索:通过 payload_types 限制消息类型
  3. 用户搜索:通过 from_uid 搜索特定用户的消息
  4. 频道搜索:通过 channel_id 搜索特定频道的消息
  5. 时间搜索:通过 start_timeend_time 限制时间范围
  6. 主题搜索:通过 topic 搜索特定主题的消息

高亮显示

通过 highlights 参数可以指定需要高亮显示的字段,搜索结果中匹配的关键词会被 <mark> 标签包围。 示例
{
  "payload": {
    "content": "北京"
  },
  "highlights": ["payload.content"]
}
返回结果:
{
  "payload": {
    "content": "你是<mark>北京</mark>大学的吗"
  }
}

使用场景

聊天记录搜索

  • 关键词搜索:用户搜索聊天记录中的关键词
  • 用户消息:搜索特定用户发送的消息
  • 群组消息:在特定群组中搜索消息

内容管理

  • 消息审核:搜索包含特定内容的消息
  • 数据分析:分析用户消息内容和行为
  • 合规检查:检查是否包含敏感内容