# Discourse 在执行搜索时应忽略字符的重音符号

**URL:** <https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198>\
**Category:** Feature\
**Tags:** search, completed\
**Created:** [2018年六月19日 10:52 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198 "2018-06-19T10:52:24Z")\
**Posts on this page:** 14\
**Page:** 3

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2018年九月17日 09:37 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/42 "2018-09-17T09:37:41Z")

</div>

This is now in with a new name 🎉

[https://github.com/discourse/discourse/commit/4481836de2feb4813b6282a6ec4ae4fdde509627](https://github.com/discourse/discourse/commit/4481836de2feb4813b6282a6ec4ae4fdde509627)

---

<div class="post-metadata">

**Author:** ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)\
**Post date:** [2018年十月2日 21:17 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/43 "2018-10-02T21:17:11Z")

</div>

Hmm, unfortunately it seems we’re not quite there yet. ☹

I see two big issues after bit of testing (upgraded today to the latest Discourse version):

1. diacritics is not elided from the query string itself, i.e. if I search with a word including diacritics, I will not find anything
2. diacritics is not stripped from post titles

---

<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:** [2018年十月17日 23:34 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/44 "2018-10-17T23:34:57Z")

</div>

> [@danekhollas](#):
>
> diacritics is not elided from the query string itself, i.e. if I search with a word including diacritics, I will not find anything

@zogstrip this feels like something we got to get sorted, @danekhollas how brave are you feeling? Do you want to try a PR?

---

<div class="post-metadata">

**Author:** ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)\
**Post date:** [2018年十月18日 01:45 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/45 "2018-10-18T01:45:49Z")

</div>

Thank you @sam for asking, I am humbled. I can do some explorations during weekend, but I want to be honest with you that in any case this would most likely require a lot of hand holding and I do not want you to spend more time with me than you would need to fix this yourselves 🙂. It’s also possible that I will hit a wall in the process and you’d have to finish up. (by which I mean I’d need to go too deep into Rails/Ruby/etc to solve the issues).

If you’re okay with that, some initial pointers where to look would be appreciated. I’ll be looking at the PRs from @zogstrip from this topic, but I don’t think that they hit all the components that might need to be modified.

Otherwise, don’t let me stop you… 🙂

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2018年十月18日 09:56 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/46 "2018-10-18T09:56:09Z")

</div>

> [@danekhollas](#):
>
> If you’re okay with that, some initial pointers where to look would be appreciated.

You’ll probably need to extract the [`strip_diacritics`](https://github.com/discourse/discourse/blob/9bf522f2278f6e6d65d4a25803d1e4b0f15395d4/app/services/search_indexer.rb#L199-L204) method so it can also be used in the [`Search.prepare_data`](https://github.com/discourse/discourse/blob/3973823a33f2215577e84e5b757f3c4f40b4cece/lib/search.rb#L49-L63) method as well when `SiteSetting.search_ignore_accents` is enabled.

---

<div class="post-metadata">

**Author:** ![asrob](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/asrob/32/186601_2.png) [@asrob](https://meta.discourse.org/u/asrob)\
**Post date:** [2018年十月19日 16:25 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/47 "2018-10-19T16:25:18Z")

</div>

There might be problems, some examples:

“álom” means “dream”.  
“alom” means “litter”.  
“rag” means “suffix” / “inflection”.  
“rág” means “chew”.  
“kar” means “arm”.  
“kár” means “damage”.

But I think end users can search more precisely, so +1 from me.

---

<div class="post-metadata">

**Author:** ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)\
**Post date:** [2018年十月19日 20:50 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/48 "2018-10-19T20:50:00Z")

</div>

Thanks for chiming in! I guess it depends on how many of these examples are there. If there are not that many, then I’d say it is always better to get more search results, albeit sometimes irrelevant, rather than getting no results at all. 🙂

---

<div class="post-metadata">

**Author:** ![Hector](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hector](https://meta.discourse.org/u/Hector)\
**Post date:** [2018年十月19日 23:20 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/49 "2018-10-19T23:20:50Z")

</div>

This can be better handled at the database level with an appropriate collation, instead of the blunt approach of stripping accents. Many databases offer accent insensitive collations for different languages. Surprisingly, compared to other databases Postgres is lacking in this respect.

Since version 10 Postgres has started incorporating support for ICU (International Components for Unicode)[1]. This library provides proper handling of accents for sorting and searching. Unfortunately not all functionality has been integrated yet. But maybe it’s worth keeping an eye on this area of Postgres development.

[1] [https://blog.2ndquadrant.com/icu-support-postgresql-10/](https://blog.2ndquadrant.com/icu-support-postgresql-10/)

---

<div class="post-metadata">

**Author:** ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)\
**Post date:** [2018年十月22日 16:22 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/50 "2018-10-22T16:22:59Z")

</div>

@zogstrip Thank you for pointers, they seem to do the trick! I’ve made a PR 🎉

[https://github.com/discourse/discourse/pull/6518](https://github.com/discourse/discourse/pull/6518)

I’ve tried adding some tests, but was generally super confused about how rspec works. They do seem to work though. 🤞

One open questions: The search log still includes accents if the `strip_diacritics` function is called from `Search.prepare_data`. I am not sure that is the desired behaviour since there will be separate logs for queries that are identical from the search perspective.

---

<div class="post-metadata">

**Author:** ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)\
**Post date:** [2018年十月23日 17:41 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/51 "2018-10-23T17:41:35Z")

</div>

Thanks Sam for merging! Just upgraded our forum and it works well.

> [@danekhollas](#):
>
> 1. diacritics is not elided from the query string itself, i.e. if I search with a word including diacritics, I will not find anything
> 2. diacritics is not stripped from post titles

I am quite confused about the second issue. I could not reproduce it in my dev environment, but it was definitely a problem on our production forum, even after upgrade. (and it was affecting new post titles as well) I eventually fixed it by rebuilding the Postgre index. i.e.

```plaintext
cd /var/discourse
./launcher enter app
rake search:reindex

```

@sam we’ve [already discussed triggering the search index rebuild](https://meta.discourse.org/t/removing-diacritics-when-tokenizing-for-search-does-not-work-retroactively/95497/4) for everyone, but I am not sure that it happened. Perhaps now is a good time after my fix was merged.

FYI for others: outstanding issues related to search w/o diacritics:

1. does not work for in-topic search
2. diacritics should not be stripped from excerpts on the search page
  - need to make sure that “word-boldening” on search page works correctly after 2. is fixed

---

<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:** [2018年十月23日 20:56 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/52 "2018-10-23T20:56:23Z")

</div>

> [@danekhollas](#):
>
> Perhaps now is a good time after my fix was merged.

I want to wait a tiny bit more, rebuild of index is quite expensive so I want to make sure I don’t dish out the cost to early.

---

<div class="post-metadata">

**Author:** ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)\
**Post date:** [2018年十月24日 03:25 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/53 "2018-10-24T03:25:41Z")

</div>

Okay, if it is tiny. 🙂 for others: if you update to tests passed right now and have a locale for which this site setting is default on, your search will be badly broken, so you need to either turn of the settings or rebuild index manually as described above.

Actually, when somebody changes this setting, it needs to trigger the rebuild, otherwise it’s just not gonna work. Is that even possible to do? @sam @zogstrip

---

<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:** [2018年十月24日 03:29 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/54 "2018-10-24T03:29:24Z")

</div>

Technically yes, we could do something like that by running a query to reset the version of the index, the trouble here is that re-indexes really are pretty expensive. Keeping this in mind though.

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [2024年二月13日 08:03 UTC](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198/56 "2024-02-13T08:03:58Z")

</div>



[上一頁](https://meta.discourse.org/t/discourse-should-ignore-if-a-character-is-accented-when-doing-a-search/90198.md?page=2)
