[英]ElasticSearch how to highlight search in nested objects?
I have a dataset like this:我有一个这样的数据集:
{
"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:
so, it goes like this page numbers and page content.所以,它就像这个页码和页面内容。 What I want to do is, for example if I search "spear", I want spears highlighted and also I want the pageNum.我想要做的是,例如,如果我搜索“spear”,我想要突出显示长矛,并且我想要 pageNum。 This is my mapping:这是我的映射:
{
"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
}
}
}
}
}
}
}
before that, "book" was not a nested object and I was getting the highlighted words with phrases but all results was together, like this:在此之前,“书”不是嵌套的 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
These are results coming from different pages, I want to know the pageNum as well.这些是来自不同页面的结果,我也想知道 pageNum。 So the result I want is something like this if I search for "Reynolds":所以如果我搜索“雷诺兹”,我想要的结果是这样的:
{
"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.” "}
What should be my search query and should I change anything in mapping or settings?我的搜索查询应该是什么,我应该更改映射或设置中的任何内容吗? How can I achieve this?我怎样才能做到这一点? Thanks in advance!提前致谢!
You can highlight the pageContent
and show the corresponding pageNum
by using highlight query in inner hits您可以通过在内部点击中使用突出显示查询来突出显示pageContent
并显示相应的pageNum
Adding a working example with index data, search query and search result添加带有索引数据、搜索查询和搜索结果的工作示例
Index Data:指数数据:
{
"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"
}
]
}
Search Query:搜索查询:
{
"query": {
"nested": {
"path": "book",
"query": {
"bool": {
"must": {
"match": {
"book.pageContent": "Reynolds"
}
}
}
},
"inner_hits": {
"highlight": {
"fields": {
"book.pageContent": {}
}
}
}
}
}
}
Search Result:搜索结果:
"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.