簡體   English   中英

ElasticSearch 如何在嵌套對象中突出顯示搜索?

[英]ElasticSearch how to highlight search in nested objects?

我有一個這樣的數據集:

{
  "took" : 29,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "kt",
        "_type" : "_doc",
        "_id" : "3kscgngBKcaOnhm7gU5w",
        "_score" : 1.0,
        "_source" : {
          "authorName" : "Alastair Reynolds",
          "bookName" : "Arınma Geçidi",
          "publishDate" : "2021",
          "book" : [
            {
              "pageNum" : 1,
              "pageContent" : ""
            },
            {
              "pageNum" : 2,
              "pageContent" : "© İndie Kitap - 2021 © The Orion Publishing Group Limited - 2019  Yazar:

所以,它就像這個頁碼和頁面內容。 我想要做的是,例如,如果我搜索“spear”,我想要突出顯示長矛,並且我想要 pageNum。 這是我的映射:

{
  "kt" : {
    "mappings" : {
      "properties" : {
        "authorName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "book" : {
          "type" : "nested",
          "properties" : {
            "pageContent" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "pageNum" : {
              "type" : "long"
            }
          }
        },
        "bookName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "publishDate" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

在此之前,“書”不是嵌套的 object 並且我得到了帶有短語的突出顯示的單詞,但所有結果都在一起,如下所示:

    """Bugün de öyle, “taptığınız <em>kitap</em> tanrısal değildir, 
el yapımıdır” diyor birileri.""",
            """“İğrenç ve utanç verici bir <em>kitap</em>” olarak tanımladığı Kuran’ı her-
kesin tanıması için (auff das yderman""",
            """Bu yazı vesilesiyle okuma fırsatı buldum, daha sonra bundan iki ay sürecek 
bir tartışma ve bir <em>kitap</em>""",
            """anlamıyorum, siz hep Kemalizm’e dair çok sert eleş-
tiriler yaptınız Yanlış Cumhuriyet diye de bir <em>kitap</em>""",
            """Siz Kemalizm’e dair derin eleştirilerde 
bulunuyordunuz, <em>kitap</em> çıktı...""",
            """Tabii  ki  ben  Yanlış  Cumhuriyet’in  önemli  bir  <em>kitap</em> 
olduğunu düşünüyorum ve özellikle de kendini""",
            """55 

這些是來自不同頁面的結果,我也想知道 pageNum。 所以如果我搜索“雷諾茲”,我想要的結果是這樣的:

            {
              "pageNum" : 3,
              "pageContent" : "Alastair <em>Reynolds</em>  ARINMA GEÇİDİ   "
            },
            {
              "pageNum" : 236,
              "pageContent" : "ne izin vererek sözlerinin hak ettikleri saygın yeri sağladı. “Peki ya  
              Thorn’un çocuğuna ne oldu?”  Khouri, “O Aura,” dedi. <em>Reynolds</em> için geldiğim çocuk.”  "}

我的搜索查詢應該是什么,我應該更改映射或設置中的任何內容嗎? 我怎樣才能做到這一點? 提前致謝!

您可以通過在內部點擊中使用突出顯示查詢來突出顯示pageContent並顯示相應的pageNum

添加帶有索引數據、搜索查詢和搜索結果的工作示例

指數數據:

{
  "authorName": "Alastair Reynolds",
  "bookName": "Arınma Geçidi",
  "publishDate": "2021",
  "book": [
    {
      "pageNum": 1,
      "pageContent": ""
    },
    {
      "pageNum": 2,
      "pageContent": "Alastair Reynolds ARINMA GEÇİDİ"
    }
  ]
}
{
  "authorName": "Alastair Reynolds",
  "bookName": "Arınma Geçidi",
  "publishDate": "2021",
  "book": [
    {
      "pageNum": 1,
      "pageContent": ""
    },
    {
      "pageNum": 2,
      "pageContent": "© İndie Kitap - 2021 © The Orion Publishing Group Limited - 2019  Yazar"
    }
  ]
}

搜索查詢:

{
  "query": {
    "nested": {
      "path": "book",
      "query": {
        "bool": {
          "must": {
            "match": {
              "book.pageContent": "Reynolds"
            }
          }
        }
      },
      "inner_hits": {
        "highlight": {
          "fields": {
            "book.pageContent": {}
          }
        }
      }
    }
  }
}

搜索結果:

"inner_hits": {
          "book": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.5619608,
              "hits": [
                {
                  "_index": "66868025",
                  "_type": "_doc",
                  "_id": "2",
                  "_nested": {
                    "field": "book",
                    "offset": 1
                  },
                  "_score": 0.5619608,
                  "_source": {
                    "pageNum": 2,              // note this
                    "pageContent": "Alastair Reynolds ARINMA GEÇİDİ"
                  },
                  "highlight": {
                    "book.pageContent": [
                      "Alastair <em>Reynolds</em> ARINMA GEÇİDİ"    // note this
                    ]
                  }
                }
              ]
            }
          }
        }
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM