【问题标题】:Highlighting the elastic search giving the results only matched突出显示仅匹配结果的弹性搜索
【发布时间】:2019-01-09 13:26:49
【问题描述】:

使用 Php 和 Elastic Search 6.5.2。对于我使用邮递员的测试场景,当我应用突出显示时,当我在突出显示下添加两个或多个片段时,仅显示与关键字句子匹配的修剪的内容。我不想在弹性搜索级别修剪内容。为此,我将片段的数量更改为零,并且在弹性搜索中得到了预期的输出,它给出了整个内容,但是当我检查 php application 中的输出时,只要匹配的关键字存在于网址字段。

索引:

PUT test/_doc/1
{
  "title":"Apply For the admissions graduate and undergraduate"
  "url":"https://someurl.com/admissions",
  "content": "Engineers play an important role in almost every aspect of modern life. As an engineer in the 21st century, you’ll work in teams to develop ingenious ways to transform the world in which we live. Industrial engineers are in high demand in nearly every industry. Astounding innovations in semiconductor microelectronic engineering will continue to drive productivity and the economy by playing a key role in a wide range of technologies – information, communication, nanotechnology, defense, medicine, and energy.Admission into the microelectronic engineering program is competitive, but our admission process is a personal one. Each application is reviewed holistically for strength of academic preparation, performance on standardized tests, counselor recommendations, and your personal career interests. We seek applicants from a variety of geographical, social, cultural, economic, and ethnic backgrounds."
}

查询:

{
   "query":{
      "query_string":{
         "fields":[
            "content"
         ],
         "query":"admissions"
      }
   },
   "highlight":{
      "fields":{
         "title":{
            "pre_tags":[
               "<strong>"
            ],
            "post_tags":[
               "</strong>"
            ],
            "number_of_fragments":3            //changed to 0 earlier
         },
         "content":{
            "pre_tags":[
               "<strong>"
            ],
            "post_tags":[
               "</strong>"
            ],
            "fragment_size":150,
            "number_of_fragments":3           //changed to 0 earlier
         }
      }
   }
}

结果:

"highlight": {
          "content": [
            "information, communication, nanotechnology, defense, medicine, and energy.Admission into the microelectronic engineering program is competitive, but our <strong>admission</strong>"
          ]
        }

【问题讨论】:

  • 弹性搜索似乎不是问题。你能在jsfiddle 上设置渲染的html吗
  • 嗨@NishantSaini 这是Url
  • 整个内容变粗是因为内容所在的&lt;div&gt;被&lt;b&gt;标签包裹。即结构是&lt;b&gt;&lt;div class="container"&gt;&lt;!-- result div here --&gt;&lt;/div&gt;&lt;/b&gt;。相反,它不应该在&lt;b&gt; 标签内。
  • 嗨@NishantSaini,我需要在我分享的PHP文件中做哪些更改my file你能指导我进行必要的更改吗?

标签: php elasticsearch


【解决方案1】:

查看您通过 cmets 共享的代码后。这个问题不在 elasticsearch 响应中。

下面这行导致了这个问题:

&lt;?php echo substr($r['highlight']['content'][0],0,300); ?&gt;

由于这个结果之一缺少关闭 &lt;/b&gt; 标记,因为您只使用前 300 个字符。

如果您注意到可以看到,您的 html 中的突出显示内容之一具有以下内容:

Tourism Management Curriculum Capstone/Exam/Thesis Options &lt;b&gt;Admissi&lt;/div&gt;

如您所见,&lt;b&gt;Admissi 缺少关闭 &lt;/b&gt; 标记,因此之后的所有内容都以粗体显示。

解决这个问题的方法可能是不使用子字符串。

【讨论】:

  • 尝试了几个选项,但没有任何建议。
  • 用echo $r['highlight']['content'][0];代替echo substr($r['highlight']['content'][0],0,300);
  • 嗨 Nishant,但这会显示整个内容,我怎样才能将内容修剪为 300 个字符。
  • 您可以使用库或编写自己的逻辑来识别子字符串中缺少的结束标记,如果缺少标记,则将其附加到子字符串中。看看this。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多