Elasticsearch Terms
https://www.elastic.co/guide/en/elastic-stack-glossary/current/terms.html
Postman
Postman is a great tool for testing Elasticsearch queries outside of your dev environment, so that you can ensure your search works before you encode it.
Queries
Here' a query which alphabetically sorts all results, but always puts a certain category at the end of the search (again in alphabetical order).
{
"query": {
"boosting": {
"positive": [
{
"bool": {
"must": [
{
"terms": {
"cultureId": [
127
]
}
},
{
"terms": {
"subsiteIds": [
105
]
}
},
{
"terms": {
"categoryIds": [
471
]
}
},
{
"terms": {
"categoryIds": [
475
]
}
},
{
"terms": {
"categoryIds": [
553
]
}
},
{
"bool": {
"should": []
}
}
]
}
}
],
"negative": {
"bool": {
"must": [
{
"terms": {
"categoryIds": [
548
]
}
}
]
}
},
"negative_boost": 0.5
}
},
"size": 20,
"from": 0,
"track_scores": true,
"sort": [
"_score",
{
"title.keyword": {
"order": "asc"
}
}
]
}
Here;s a query as above without the boosting:
{
"query": {
"bool": {
"must": [
{
"terms": {
"cultureId": [
127
]
}
},
{
"terms": {
"subsiteIds": [
105
]
}
},
{
"terms": {
"categoryIds": [
471
]
}
},
{
"terms": {
"categoryIds": [
475
]
}
},
{
"terms": {
"categoryIds": [
553
]
}
},
{
"bool": {
"should": [
{
"term": {
"categoryIds": 563
}
},
{
"term": {
"categoryIds": 564
}
}
]
}
}
]
}
},
"size": 20,
"from": 0,
"sort": [
{
"title.keyword": {
"order": "asc"
}
}
]
}
Here's a query which yields the same results as above, but with using the "filter" term. This was already coded in to the js file I've been working on, so at some point I will find out what the requirement, if any, for this term was.
{
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"terms": {
"cultureId": [
127
]
}
},
{
"terms": {
"subsiteIds": [
105
]
}
},
{
"terms": {
"categoryIds": [
471
]
}
},
{
"terms": {
"categoryIds": [
475
]
}
},
{
"terms": {
"categoryIds": [
553
]
}
},
{
"bool": {
"should": [
{"term": {"categoryIds": 563}},
{"term": {"categoryIds": 564}}
]
}
}
]
}
}
]
}
},
"size": 20,
"from": 0,
"sort": [
{
"title.keyword": {
"order": "asc"
}
}
]
}
Created: 12-Feb-2023