Hi there,
I am new to ElasticSearch and still not much familiar with the capabilities of the query language. Am I trying to perform a specific query and would appreciate some help on how to build it, even the answer is that I cannot achieve that using ElasticSearch.
I am storing documents with the following format on my ElasticSearch cluster:
{
"id" : 996,
"price" : 320,
"company" : "Company A",
"created_at" : "2020-11-21T17:02:15.707Z",
"updated_at" : "2020-11-21T17:02:15.707Z",
"departments" : [
{
"id" : 10,
"name" : "Insurance"
},
{
"id" : 129,
"name" : "Information Technology and Services"
}
],
"source" : "database"
}
And these are my mappings:
settings do
mapping do
indexes :company, type: :text
indexes :price, type: :integer
indexes :departments, type: :object do
indexes :id, type: :keyword
end
end
end
Given a search term and an array of department ids, I want to create a score function that achieves the following behavior:
- First, return all the records where the search term is an exact match with the company field and the array of department ids has at least one match with departments.id fields.
- After these, come all the records where the search term has an exact match with the company field.
- In the sequence, return at maximum 5 elements with source = api.
- At the end, come all the other records ordered by price (ascending).
I started with something like this:
{
query: {
bool: {
must: [
{ match_all: {} }
],
should: [
{ term: { 'company.keyword': { value: <searchterm> } }, boost: 4 },
{ terms: { 'departments.id': [<department_ids>], boost: 3 } },
{ term: { source: { value: 'api', boost: 2 } }
]
}
},
sort: [
'_score',
{ price: { order: 'desc' } }
]
}
Is there a way to limite the results of a specific leaf query? Something like this:
{ term: { source: { value: 'api', boost: 2, size: 5 } }
Can someone please help me clarify the major misconceptions I am making? I'm sure there are several.
Also, any help to make my query come closer to the desired result is welcome.
Thanks.