在突出显示我的应用程序的搜索时,如果我们传递pageSize,它只会从记录中返回一个匹配的字段。例如有4条记录
剩余信息添加在评论中,因为我无法在这里添加代码,请建议我如何实现这一要求
如果您需要突出显示每个文档的所有匹配字段的结果,那么您可以通过以下方式实现:
添加一个具有索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 50
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer"
},
"lastname": {
"type": "text",
"analyzer": "my_analyzer"
},
"firstname": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
指数数据:
{
"name": "jhon",
"Age": 24,
"lastname": "willam",
"firstname": "henry"
}
{
"name": "kellin",
"Age": 24,
"lastname": "kevin",
"firstname": "mathew"
}
{
"name": "keeper",
"Age": 24,
"lastname": "Jr",
"firstname": "gomez"
}
{
"name": "Asif",
"Age": 24,
"lastname": "peter",
"firstname": "willaim kemp"
}
搜索查询:
{
"query": {
"multi_match": {
"query": "ke"
}
},
"highlight": {
"fields": {
"*": {
"type": "plain",
"fragment_size": 20,
"pre_tags": "<span class='bold'>",
"post_tags": "</span>",
"number_of_fragments": 1
}
}
}
}
搜索结果:
"hits": [
{
"_index": "64996939",
"_type": "_doc",
"_id": "2",
"_score": 1.1374959,
"_source": {
"name": "kellin",
"Age": 24,
"lastname": "kevin",
"firstname": "mathew"
},
"highlight": {
"name": [
"<span class='bold'>ke</span>llin"
], <-- note this
"lastname": [
"<span class='bold'>ke</span>vin"
]
}
},
{
"_index": "64996939",
"_type": "_doc",
"_id": "4",
"_score": 0.9552834,
"_source": {
"name": "Asif",
"Age": 24,
"lastname": "peter",
"firstname": "willaim kemp"
},
"highlight": {
"firstname": [
"willaim <span class='bold'>ke</span>mp" <-- note this
]
}
},
{
"_index": "64996939",
"_type": "_doc",
"_id": "3",
"_score": 0.62883455,
"_source": {
"name": "keeper",
"Age": 24,
"lastname": "Jr",
"firstname": "gomez"
},
"highlight": {
"name": [
"<span class='bold'>ke</span>eper" <--note this
]
}
}
]