首页 > 解决方案 > 突出显示仅匹配结果的弹性搜索

问题描述

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

指数:

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>"
          ]
        }

标签: phpelasticsearch

解决方案


查看您通过评论共享的代码后。此问题不在弹性搜索响应中。

以下行导致问题:

<?php echo substr($r['highlight']['content'][0],0,300); ?>

因此,其中一个结果是缺少结束</b>标签,因为您只使用了前 300 个字符。

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

Tourism Management Curriculum Capstone/Exam/Thesis Options <b>Admissi</div>

如您所见,结束</b>标签丢失了<b>Admissi,因此之后的所有内容都以粗体显示。

对此的解决方案可能是不使用子字符串。


推荐阅读