I am fairly new to ElasticSearch and getting my feet wet so please pardon if I missed any documentation and/or cannot see/understand the usage clearly.
Now, on-to the problem. Following is a simple default mapping I am using to index documents (products) with their associated brand name.
$params = [
'index' => 'test_index',
'body' => [
'mappings' => [
'_default_' => [
'properties' => [
'brand' => [
'properties' => [
'name' => [
'type' => 'text',
'fields' => [
'keyword' => [
'type' => 'keyword',
'ignore_above' => 256
]
]
],
'private' => [
'type' => 'byte',
'fields' =>[
'keyword' => [
'type' => 'keyword',
'ignore_above' => 256
]
]
]
]
],
'product' => [
'properties' => [
'id' => [
'type' => 'integer',
'index' => 'not_analyzed'
],
'name' => [
'type' => 'text',
'fields' => [
'keyword' => [
'type' => 'keyword',
'ignore_above' => 256
]
]
]
]
]
]
]
]
]
];
The private field has 2 possible values 1
and 0
.
As part of the search query, I am trying to push private brands (brands.private) (1
) before the non-private brands (0
).
Using the query below,
'{
"index": "test_index",
"type": "test_type",
"explain": true,
"body": {
"from": 0,
"size": 20,
"query": {
"bool": {
"must": {
"0": {
"multi_match": {
"query": "some_query",
"type": "phrase",
"fields": {
"1": "product.name^4",
"2": "brand.name^3"
}
}
}
},
"should": {
"term": {
"brand.private": {
"value": 1,
"boost": 5
}
}
}
}
}
}
}'
I am able to boost score but the results still show up non private brands and rightly so since the overall score is still higher than private brands. This makes sense but I need to show private brands first and non private after them. Is there a way to accomplish this?
Another possible alternative I came across was the use of function score value but am not sure if it's the right way to go.