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. 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:
"""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. 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
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
]
}
}
]
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.