Skip to content

Commit

Permalink
Highlight index fields of type text (#50)
Browse files Browse the repository at this point in the history
* Highlight index fields of type text
  • Loading branch information
hayco authored May 1, 2024
1 parent 90e1c99 commit ac8a2f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target/
/.make
*.json
*.lst
*.log
globalise.http
/globalise.http
/mondriaan.http
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/nl/knaw/huc/broccoli/api/ElasticQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ data class Sort(

data class HighlightTerm(
@JsonIgnore val text: String,
@JsonIgnore val fragmentSize: Int
@JsonIgnore val fragmentSize: Int,
@JsonIgnore val extraFields: List<String>? = null
) {
@JsonAnyGetter
fun toJson() = mapOf(
"fields" to mapOf(
"fields" to mutableMapOf(
"text" to mapOf(
"type" to "unified",
"fragment_size" to fragmentSize,
)
),
).apply {
extraFields?.forEach {
put(it, mapOf("type" to "unified"))
}
},
"highlight_query" to FullTextQuery(QueryString(text))
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ class ElasticQueryBuilder(private val index: IndexConfiguration) {
)
),

highlight = query.text
?.let { HighlightTerm(it, fragmentSize) },
highlight = query.text?.let { queryText ->
HighlightTerm(
text = queryText,
fragmentSize = fragmentSize,
extraFields = index.fields.filter { it.type == "text" }.map { it.name }
)
},

aggregations = (query.aggregations ?: index.fields.map { it.name })
.mapNotNull { fieldName ->
Expand Down

0 comments on commit ac8a2f0

Please sign in to comment.