程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如果将min_gram设置为1,则在ngram过滤器上突出显示Elasticsearch是很奇怪的大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如果将min_gram设置为1,则在Ngram过滤器上突出显示Elasticsearch是很奇怪的?

开发过程中遇到如果将min_gram设置为1,则在Ngram过滤器上突出显示Elasticsearch是很奇怪的的问题如何解决?下面主要结合日常开发的经验,给出你关于如果将min_gram设置为1,则在Ngram过滤器上突出显示Elasticsearch是很奇怪的的解决方法建议,希望对你解决如果将min_gram设置为1,则在Ngram过滤器上突出显示Elasticsearch是很奇怪的有所启发或帮助;

简短答案

您需要检查 并查看是否使用fast-vector-Highlighter。但是,您仍然需要非常小心自己的查询。

详细答案

假设使用ES的新实例0.20.4localhost

以您的示例为基础,让我们添加显式映射。注意我为该code字段设置了两种不同的分析。唯一的区别是"term_vector":"with_positions_offsets"

curl -X PUT localhost:9200/myindex -d '
{
  "setTings" : {
    "index":{
      "number_of_replicas":0,
      "number_of_shards":1,
      "analysis":{
        "analyzer":{
          "default":{
            "type":"custom",
            "tokenizer":"keyword",
            "filter":[
              "lowercase",
              "my_ngram"
            ]
          }
        },
        "filter":{
          "my_ngram":{
            "type":"nGram",
            "min_gram":1,
            "max_gram":20
          }
        }
      }
    }
  },
  "mapPings" : {
    "product" : {
      "propertIEs" : {
        "code" : {
          "type" : "multi_fIEld",
          "fIElds" : {
            "code" : {
              "type" : "String",
              "analyzer" : "default",
              "store" : "yes"
            },
            "code.ngram" : {
              "type" : "String",
              "analyzer" : "default",
              "store" : "yes",
              "term_vector":"with_positions_offsets"
            }
          }
        }
      }
    }
  }
}'

索引一些数据。

curl -X POST 'localhost:9200/myindex/product' -d '{
  "code" : "Samsung galaxy i7500"
}'

curl -X POST 'localhost:9200/myindex/product' -d '{
  "code" : "Samsung galaxy 5 Europa"
}'

curl -X POST 'localhost:9200/myindex/product' -d '{
  "code" : "Samsung galaxy Mini"
}'

现在我们可以运行查询了。

1)搜索“ i”以查看带有突出显示的字符搜索功能

curl -X GET 'localhost:9200/myindex/product/_search?pretty' -d '{
  "fIElds" : [ "code" ],
  "query" : {
    "term" : {
      "code" : "i"
    }
  },
  "highlight" : {
    "number_of_fragments" : 0,
    "fIElds" : {
      "code":{},
      "code.ngram":{}
    }
  }
}'

这将产生两个搜索结果:

# 1
...
"fIElds" : {
  "code" : "Samsung galaxy Mini"
},
"highlight" : {
  "code.ngram" : [ "Samsung galaxy M<em>i</em>n<em>i</em>" ],
  "code" : [ "Samsung galaxy M<em>i</em>n<em>i</em>" ]
}
# 2
...
"fIElds" : {
  "code" : "Samsung galaxy i7500"
},
"highlight" : {
  "code.ngram" : [ "Samsung galaxy <em>i</em>7500" ],
  "code" : [ "Samsung galaxy <em>i</em>7500" ]
}

这次codecode.ngem字段均正确突出显示。但是当使用更长的查询时,情况会迅速改变:

2)搜索“ ym

curl -X GET 'localhost:9200/myindex/product/_search?pretty' -d '{
  "fIElds" : [ "code" ],
  "query" : {
    "term" : {
      "code" : "y m"
    }
  },
  "highlight" : {
    "number_of_fragments" : 0,
    "fIElds" : {
      "code":{},
      "code.ngram":{}
    }
  }
}'

这样产生:

"fIElds" : {
  "code" : "Samsung galaxy Mini"
},
"highlight" : {
  "code.ngram" : [ "Samsung galax<em>y M</em>ini" ],
  "code" : [ "Samsung galaxy Min<em>y M</em>i" ]
}

code领域是不正确高亮(类似你的情况)。

重要的一点是, 一词代替了 。

解决方法

所以我有这个索引

{
  "setTings":{
    "index":{
      "number_of_replicas":0,"analysis":{
        "analyzer":{
          "default":{
            "type":"custom","tokenizer":"keyword","filter":[
              "lowercase","my_ngram"
            ]
          }
        },"filter":{
          "my_ngram":{
            "type":"nGram","min_gram":2,"max_gram":20
          }
        }
      }
    }
  }
}

我正在通过轮胎宝石进行搜索

{
   "query":{
      "query_String":{
         "query":"xyz","default_operator":"AND"
      }
   },"sort":[
      {
         "count":"desc"
      }
   ],"filter":{
      "term":{
         "active":true,"_type":null
      }
   },"highlight":{
      "fields":{
         "name":{

         }
      },"pre_tags":[
         "<strong>"
      ],"post_tags":[
         "</strong>"
      ]
   }
}

并且我有两个应该匹配的帖子,分别名为“ xyz post”和“ xyz问题”。执行此搜索时,我正确地获得了突出显示的字段

<strong>xyz</strong> question
<strong>xyz</strong> post

现在这就是事情……一旦我在索引和重新索引中将min_gram更改为1。突出显示的字段开始像这样返回

<strong>x</strong><strong>y</strong><strong>z</strong> pos<strong>xyz</strong>t
<strong>x</strong><strong>y</strong><strong>z</strong> questio<strong>xyz</strong>n

我根本不明白为什么。

大佬总结

以上是大佬教程为你收集整理的如果将min_gram设置为1,则在ngram过滤器上突出显示Elasticsearch是很奇怪的全部内容,希望文章能够帮你解决如果将min_gram设置为1,则在ngram过滤器上突出显示Elasticsearch是很奇怪的所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。