Reputation: 67
I'm very new to elastic search, how do I write a query which search for a keyword (ie. test keyword) in all fields in the document, and boost for
I see some documentation on match_phrase, but it doesn't seem to support multiple fields.
{
"query": {
"multi_match": {
"query": "test keyword",
"fields": ["A^5", "B^3", "C^1"]
}
}
}
Upvotes: 0
Views: 400
Reputation: 16192
If you want an exact match for the keyword phrase in all fields along with boost then try out this below search query where the multi-match query is used with type phrase
parameter :
Adding a working example with index data, search query, and search result
Index data:
{
"A":"test keyword",
"B":"a",
"C":"c"
}
{
"A":"a",
"B":"test keyword",
"C":"c"
}
{
"A":"a",
"B":"b",
"C":"test keyword"
}
Search Query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "test keyword",
"fields": [
"A^5",
"B^3",
"C^1"
],
"type":"phrase" <-- note this
}
}
]
}
}
}
Search Result:
"hits": [
{
"_index": "stof_64266554",
"_type": "_doc",
"_id": "1",
"_score": 16.285465,
"_source": {
"A": "test keyword",
"B": "a",
"C": "c"
}
},
{
"_index": "stof_64266554",
"_type": "_doc",
"_id": "2",
"_score": 8.142733,
"_source": {
"A": "a",
"B": "test keyword",
"C": "c"
}
},
{
"_index": "stof_64266554",
"_type": "_doc",
"_id": "3",
"_score": 1.6285465,
"_source": {
"A": "a",
"B": "b",
"C": "test keyword"
}
}
]
Upvotes: 1