首页 > 解决方案 > 如何在弹性搜索的突出显示部分应用布尔值(必须,应该)?

问题描述

我正在尝试在查询中应用必须/应该,但突出显示的部分没有给出预期的结果。在弹性搜索参考中,我知道了这一点, Highlighters don’t reflect the boolean logic of a query when extracting terms to highlight但我想在我的亮点中应用布尔逻辑。怎么做?

这是我的索引

PUT myindex/_doc/1
{
    "name" : "ram",
    "lastname" : "asb",
    "job_description" : "Systems administrator and Linux specialit",
   "sections":[
       {
         "text":"  provided, that Holdings may incur Indebtedness  or issue shares of Disqualified Stock, and any Restricted Subsidiary may incur Indebtedness  and issue shares of Disqualified Stock and any Restricted Subsidiary that is not the Issuer or a Subsidiary Guarantor may issue shares of Preferred Stock, if (i) the  on a consolidated basis of Holdings and its Restricted Subsidiaries for the most recently ended four fiscal quarters for which internal financial statements are available immediately preceding the date on which such additional Indebtedness is incurred or such Disqualified Stock or Preferred Stock is issued would  (ii) the Consolidated  on a consolidated basis of Holdings and its Restricted Subsidiaries for the most recently ended four fiscal quarters for which internal financial statements are available immediately preceding the date on which such additional Indebtedness is incurred or such  been equal"
       },
       {
         "text":"Indebtedness incurred pursuant to any by Holdings or any Restricted Subsidiary and the  and creation of  and bankers’ acceptances thereunder ; provided that immediately after giving effect to any such incurrence or issuance (including pro forma application of the net proceeds therefrom), the then outstanding aggregate principal amount of all Indebtedness incurred or issued under this clause (1) does not exceed the sum  an additional amount after all amounts have been incurred under clause (1)(a), if after giving pro forma effect to the incurrence of such additional amount (including a pro forma application of the net proceeds therefrom), the Consolidated  would have been equal; provided that for purposes of determining the amount that may be incurred under this clause (1)(b) only, all Indebtedness incurred under this clause (1)(b) shall be deemed to be included in clause (1) of the definition of “Consolidated "
       }
    ] 
} 

这是我的查询

GET myindex/_search
{
  "_source": false, 
  "query":{
    "nested":{
      "path": "sections",
      "query":{
        "bool":{
          "must": [
            {
               "match":{"sections.text":"Consolidated"}
            },
            {
               "match":{"sections.text":"forma"}
            }
          ]
        }
      }
    }
  },
    "highlight": {
       "fragment_size" : 200,
      "no_match_size": 0, 
        "fields" : {
          "sections.text" : {}
        }
    }
}

这是我的输出

"hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3312968,
        "highlight" : {
          "sections.text" : [
            "on a <em>consolidated</em> basis of Holdings and its Restricted Subsidiaries for the most recently ended four fiscal quarters for which internal financial statements are available immediately preceding the date",
            "by Holdings or any Restricted Subsidiary and the  and creation of  and bankers’ acceptances thereunder ; provided that immediately after giving effect to any such incurrence or issuance (including pro <em>forma</em>",
            "principal amount of all Indebtedness incurred or issued under this clause (1) does not exceed the sum  an additional amount after all amounts have been incurred under clause (1)(a), if after giving pro <em>forma</em>",
            "effect to the incurrence of such additional amount (including a pro <em>forma</em> application of the net proceeds therefrom), the <em>Consolidated</em>  would have been equal; provided that for purposes of determining",
            "the amount that may be incurred under this clause (1)(b) only, all Indebtedness incurred under this clause (1)(b) shall be deemed to be included in clause (1) of the definition of “&lt;em>Consolidated</em>"
          ]
        }
      }
    ]

我们可以清楚地看到,它不会输出为(合并的 AND 形式)

如果有人查询 a AND b

GET index1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "FIELD": "a"
          }
        },
        {
          "match": {
            "FIELD": "b"
          }
        }
      ]
    }
  },
    "highlight": {
       "fragment_size" : 200,
      "no_match_size": 0, 
        "fields" : {
          "sections.text" : {}
        }
    }
}

我的愿望输出将是

"hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.371541,
        "highlight" : {
          "sections.text" : [
            "fgfg <em>a</em> fgrgth <em>b</em>"
         ]
        }
      }
    ]

标签: elasticsearchlucene

解决方案


推荐阅读