# 中文搜索摘要似乎已损坏

**URL:** https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866
**Category:** Bug
**Tags:** pr-welcome
**Created:** [2020年十一月11日 21:39 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866 "2020-11-11T21:39:43Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![haroldfy](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@haroldfy](https://meta.discourse.org/u/haroldfy)
#### Post date: [2020年十一月11日 21:39 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/1 "2020-11-11T21:39:43Z")

</div>

当我在论坛中尝试搜索中文时，返回的搜索结果句子破碎，标点符号缺失，词语之间出现了意外的空格，甚至还有一些词语丢失。

例如，我尝试搜索 `管理员`，原句是：

> 管理人员可见的分类。只有管理员和版主才能阅览主题

但在搜索结果中看到的却是下面这样。

 ![Screen Shot 2020-11-11 at 1.21.34 PM](https://global.discourse-cdn.com/meta/original/3X/f/e/fe90a6a0de285978fd423d1127850d0fac4ca56d.png)

如您所见，`可见的` 缺失了，句号 `。` 也缺失了，导致句子断裂。此外，`只有`、` 和` 以及 `才能` 也丢失了，并且词语之间出现了意外的空格。

有人能帮我解决这个问题吗？谢谢

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2020年十二月3日 02:12 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/4 "2020-12-03T02:12:10Z")

</div>

看起来那些缺失的字符在中文里被视为停用词。

```ruby
(byebug) data = CppjiebaRb.segment(search_data, mode: mode)
["管理人员", "可见", "的", "分类", "。", "只有", "管理员", "和", "版主", "才能", "阅览", "主题"]
(byebug) CppjiebaRb.filter_stop_word(data)
["管理人员", "分类", "管理员", "版主", "阅览", "主题"]

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2020年十二月3日 05:07 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/5 "2020-12-03T05:07:19Z")

</div>

等等，这里的 bug 是指结果中的“摘要”显示异常吗？而不是搜索功能本身存在实际问题？

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2020年十二月4日 00:51 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/6 "2020-12-04T00:51:35Z")

</div>

是的，搜索功能仍然正常，只是显示的摘要不够理想。对于中文，搜索的处理方式有所不同。我们不是在搜索查询中忽略停用词，而是完全将其从搜索数据中排除。

---

<div class="post-metadata">

### Author: ![haroldfy](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@haroldfy](https://meta.discourse.org/u/haroldfy)
#### Post date: [2020年十二月6日 18:58 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/7 "2020-12-06T18:58:22Z")

</div>

感谢您对此进行调查。

> [@haroldfy](#):
>
> 可见的

这不是中文中的停用词，而是一个形容词，意思是“可见的”。

能否修复此问题（包括搜索结果中的所有内容）？或者有什么变通方法吗？

谢谢。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2020年十二月6日 20:16 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/8 "2020-12-06T20:16:35Z")

</div>

停用词指的是那些非常常见且会损害搜索性能的词语。

例如，在英语中，“And”就是一个停用词。

这个问题是可以修复的，预计需要几个月时间才能完成。在此期间，如果您需要快速修复，可以参考 #marketplace。

标记为 #pr-welcome。

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2020年十二月8日 01:22 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/9 "2020-12-08T01:22:07Z")

</div>

> [@haroldfy](#):
>
> 这在中文中不是停用词

停用词是通过 [GitHub - yanyiwu/cppjieba: "结巴"中文分词的C++版本 · GitHub](https://github.com/yanyiwu/cppjieba) 确定的。鉴于最近对搜索摘要显示方式的更改，我们应该直接删除以下行，因为它干扰了实际的搜索数据。

> <https://github.com/discourse/discourse/blob/1cf057fb1c4e168ce441ddde918636725abeb668/lib/search.rb#L75>

无论如何，我们目前对中文的搜索支持并不理想，但我们可以考虑使用 PostgreSQL 扩展，以便更好地支持那些没有原生支持的语言。例如 [https://pgroonga.github.io/](https://pgroonga.github.io/)?

---

<div class="post-metadata">

### Author: ![haroldfy](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@haroldfy](https://meta.discourse.org/u/haroldfy)
#### Post date: [2020年十二月9日 01:21 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/10 "2020-12-09T01:21:44Z")

</div>

谢谢！让我试试看效果如何。

---

<div class="post-metadata">

### Author: ![yiksanchan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yiksanchan/32/202094_2.png) [@yiksanchan](https://meta.discourse.org/u/yiksanchan)
#### Post date: [2020年十二月18日 07:08 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/11 "2020-12-18T07:08:37Z")

</div>

@tgxworld 我不确定我是否理解正确，PGroonga 不支持中文和日文。在 [https://pgroonga.github.io/](https://pgroonga.github.io/) 中：

> PostgreSQL 仅支持对使用字母和数字的语言进行全文搜索。这意味着 PostgreSQL 不支持对日文、中文等语言进行全文搜索。通过在 PostgreSQL 中安装 PGroonga，您可以对所有语言使用超高速的全文搜索功能！

---

<div class="post-metadata">

### Author: ![yiksanchan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yiksanchan/32/202094_2.png) [@yiksanchan](https://meta.discourse.org/u/yiksanchan)
#### Post date: [2020年十二月18日 07:21 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/12 "2020-12-18T07:21:11Z")

</div>

@tgxworld 按照您的建议，我已创建了一个 PR：[FIX: keep chinese stopwords on search by t0t07 · Pull Request #11530 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/11530)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2020年十二月18日 20:06 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/13 "2020-12-18T20:06:47Z")

</div>

实际情况恰恰相反。普通的 PostgreSQL 不支持中文和日文，而 PGroonga 则增加了对这些语言的支持。

---

<div class="post-metadata">

### Author: ![yiksanchan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yiksanchan/32/202094_2.png) [@yiksanchan](https://meta.discourse.org/u/yiksanchan)
#### Post date: [2020年十二月18日 20:14 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/15 "2020-12-18T20:14:08Z")

</div>

顺便提一下 @riking，想确认一下，目前 Discourse 是使用 PostgreSQL 内置函数来实现全文搜索的，就像这里：[discourse/lib/search.rb at 1cf057fb1c4e168ce441ddde918636725abeb668 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/1cf057fb1c4e168ce441ddde918636725abeb668/lib/search.rb#L911)

是这样吗？

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2020年十二月21日 01:25 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/16 "2020-12-21T01:25:58Z")

</div>

> [@yiksanchan](#):
>
> 我按照您的建议创建了一个 PR [在搜索中保留中文停用词 by YikSanChan · 拉取请求 #11530 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/11530)

抱歉，之前没有说得更具体。如果我们现在真的想修复这个问题，我们需要确保在搜索数据中不删除中文的停用词，同时确保在作为搜索查询使用时仍会删除停用词。

---

<div class="post-metadata">

### Author: ![yiksanchan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yiksanchan/32/202094_2.png) [@yiksanchan](https://meta.discourse.org/u/yiksanchan)
#### Post date: [2020年十二月21日 01:37 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/17 "2020-12-21T01:37:50Z")

</div>

@tgxworld 我不太明白搜索数据与搜索查询之间的区别。能否请您提供更多详细信息？谢谢。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2020年十二月21日 01:46 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/18 "2020-12-21T01:46:15Z")

</div>

如果我们将停用词加入索引，会导致索引膨胀，并降低搜索性能。

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2020年十二月21日 03:01 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/19 "2020-12-21T03:01:35Z")

</div>

> <https://github.com/discourse/discourse/blob/d2a04621862aa7f7fc283112d542648e9f3fcab8/app/models/post_search_data.rb#L12-L13>

`PostSearchData` 表中存储了两列数据：`#search_data` 用于根据搜索词进行查询，`#raw_data` 则用于显示搜索摘要。此处的修复方案应为：在从 `#search_data` 中移除中文停用词的同时，保留 `#raw_data` 中的中文停用词。

---

<div class="post-metadata">

### Author: ![th21](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/th21/32/211915_2.png) [@th21](https://meta.discourse.org/u/th21)
#### Post date: [2021年五月6日 05:02 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/20 "2021-05-06T05:02:09Z")

</div>

这个bug有进展了吗？

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2021年五月20日 05:50 UTC](https://meta.discourse.org/t/chinese-search-excerpts-appear-broken/169866/21 "2021-05-20T05:50:01Z")

</div>

我以为我在这里做了一些修改：

> <https://github.com/discourse/discourse/blob/626b8465baed15799b89135d79c9b8a00eda3bb7/lib/search.rb#L67-L70>

您的语言环境是否设置为 `zh_TW`、`zh_CN` 或 `ja`？如果不是，`search_tokenize_chinese_japanese_korean` 是否设置为 true？

我们在这里有一个绕过方案：

> <https://github.com/discourse/discourse/blob/626b8465baed15799b89135d79c9b8a00eda3bb7/lib/search/grouped_search_results.rb#L90-L104>
