【问题标题】:Searchkick highlights unnecessary wordSearchkick 突出显示不必要的单词
【发布时间】:2026-01-14 13:05:01
【问题描述】:

我正在突出显示不必要的单词,例如我正在搜索“Document CASE No. 2015-331” 这里是 searchkick 将突出显示的列表

  1. “文件 CASE No. 2015-331”
  2. “不是”
  3. “不”
  4. “开”
  5. “case is”
  6. 高亮大声笑
  7. “2015”
  8. “2017”
  9. “2018”
  10. “2016”
  11. “到”
  12. “不”

这是我的搜索结果

    search = ::Document.search params[:q], fields: [:content], where: {id:
     params[:id]}, highlight: { tag: 'span class=match-matcher',
     fragment_size: @document.content.length}

    search.with_highlights.each do |document, highlights|

       document.content = highlights[:content]

    end

此处的目标是仅突出显示“文件 CASE No. 2015-331”

【问题讨论】:

    标签: ruby-on-rails elasticsearch searchkick


    【解决方案1】:

    看起来您的字段在编入索引时已经过分析。 如果您想实现完全匹配并且标记应该是可搜索的,则映射应该是“not_analyzed”并且需要重新索引数据。

    在这里您正在寻找完全匹配。

    通过添加如下内容为您的字段修改映射。

    "mappings": {
        "properties": {
           "city": {
            "type": "text",
             "fields": {
               "raw": { 
                 "type":  "keyword"
               }
              }
            }
          }
        }
    

    【讨论】:

    • 我应该把映射放在哪里?
    • 你给我的想法兄弟谢谢我想接受你的答案,但我正在使用 Searchkick 这可能会接受你的答案,但我会给出我自己的答案?
    • 最后一个问题我有两个参数我有q和id我应该把它放在哪里?
    • 查询时不能放(假设)...以上设置是在数据索引到elasticsearch之前完成的...
    最近更新 更多