| Query type |
Solr |
OpenSearch |
Basic Boolean query |
Solr: # Simple field query
q=title:java
# Multiple field queries
q=title:java AND author:smith
q=title:java OR author:python
|
// Simple field query
{
"query": {
"term": {
"title": "java"
}
}
}
// Multiple field queries - AND
{
"query": {
"bool": {
"must": [
{"term": {"title": "java"}},
{"term": {"author": "smith"}}
]
}
}
}
// Multiple field queries - OR
{
"query": {
"bool": {
"should": [
{"term": {"title": "java"}},
{"term": {"author": "python"}}
]
}
}
}
|
Full-text search queries with boost |
# Solr DisMax
q=java programming&defType=dismax&qf=title^2 content author
# Solr eDisMax with phrase boost
q=java programming&defType=edismax&qf=title^3 content^1&pf=title^10&mm=75%
|
// Basic multi-match
{
"query": {
"multi_match": {
"query": "java programming",
"fields": ["title^2", "content", "author"]
}
}
}
// Advanced multi-match with phrase boost
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "java programming",
"fields": ["title^3", "content"],
"minimum_should_match": "75%"
}
}
],
"should": [
{
"match_phrase": {
"title": {
"query": "java programming",
"boost": 10
}
}
}
]
}
}
}
|
Range queries |
# Numeric range
q=price:[10 TO 50]
# Date range
q=publishDate:[2020-01-01T00:00:00Z TO 2023-12-31T23:59:59Z]
# Open-ended range
q=price:[10 TO *]
q=price:[* TO 50]
|
// Numeric range
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 50
}
}
}
}
// Date range
{
"query": {
"range": {
"publishDate": {
"gte": "2020-01-01T00:00:00Z",
"lte": "2023-12-31T23:59:59Z"
}
}
}
}
// Open-ended range
{
"query": {
"range": {
"price": {
"gte": 10
}
}
}
}
|
Wildcard and fuzzy queries |
# Wildcard queries
q=title:jav*
q=title:*ava
q=title:j?va
# Fuzzy queries
q=title:java~2
q=title:"java programming"~3
|
// Wildcard queries
{
"query": {
"wildcard": {
"title": "jav*"
}
}
}
{
"query": {
"wildcard": {
"title": "*ava"
}
}
}
// Fuzzy queries
{
"query": {
"fuzzy": {
"title": {
"value": "java",
"fuzziness": 2
}
}
}
}
// Fuzzy phrase query
{
"query": {
"match_phrase": {
"title": {
"query": "java programming",
"slop": 3
}
}
}
}
|
Filter queries |
# Filter queries
q=java&fq=category:programming&fq=inStock:true&fq=price:[10 TO 50]
|
{
"query": {
"bool": {
"must": [
{"match": {"_all": "java"}}
],
"filter": [
{"term": {"category": "programming"}},
{"term": {"inStock": true}},
{"range": {"price": {"gte": 10, "lte": 50}}}
]
}
}
}
|
Phrase and proximity queries |
# Exact phrase
q="java programming"
# Proximity search
q="java programming"~5
# Phrase with field
q=title:"machine learning"~2
|
// Exact phrase
{
"query": {
"match_phrase": {
"_all": "java programming"
}
}
}
// Proximity search
{
"query": {
"match_phrase": {
"_all": {
"query": "java programming",
"slop": 5
}
}
}
}
// Phrase with specific field
{
"query": {
"match_phrase": {
"title": {
"query": "machine learning",
"slop": 2
}
}
}
}
You can also use span and interval queries for proximity
search. |
Aggregation |
# Basic faceting
q=*:*&facet=true&facet.field=category&facet.field=author
|
// Basic aggregations
{
"query": {"match_all": {}},
"aggs": {
"categories": {
"terms": {
"field": "category"
}
},
"authors": {
"terms": {
"field": "author"
}
}
}
}
|
Nested faceting or aggregation |
# nested faceting
{
"query": "*:*",
"facet": {
"categories": {
"type": "terms",
"field": "category",
"limit": 10,
"facet": {
"brands": {
"type": "terms",
"field": "brand",
"limit": 5,
"facet": {
"avg_price": {
"type": "avg",
"field": "price"
},
"max_price": {
"type": "max",
"field": "price"
}
}
}
}
}
}
}
|
# OpenSearch nested aggregation
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"categories": {
"terms": {
"field": "category",
"size": 10
},
"aggs": {
"brands": {
"terms": {
"field": "brand",
"size": 5
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"max_price": {
"max": {
"field": "price"
}
}
}
}
}
}
}
}
|
Geospatial queries |
# Geo distance
q={!geofilt pt=37.7752,-122.4232 sfield=location d=10}
|
// Geo distance
{
"query": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 37.7752,
"lon": -122.4232
}
}
}
}
|