Last active
June 26, 2019 20:21
-
-
Save egaumer/4519444 to your computer and use it in GitHub Desktop.
Some example queries using elastic.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# simple match all query with term facet | |
ejs.Request() | |
.indices("myindex") | |
.types("mytype") | |
.query(ejs.MatchAllQuery()) | |
.facet( | |
ejs.TermsFacet('url') | |
.field('url') | |
.size(20)) | |
# filtered query with GeoDistance filter | |
ejs.Request() | |
.indices("myindex") | |
.types("mytype") | |
.query( | |
ejs.FilteredQuery( | |
ejs.MatchAllQuery(), | |
ejs.GeoDistanceFilter("location") | |
.distance(10) | |
.point(ejs.GeoPoint([40.53555, -75.57655])))) | |
# an advanced dismax query used to return "featured results" | |
ejs.Request() | |
.indices("myindex") | |
.types("mytype") | |
.query( | |
ejs.BoolQuery() | |
.should( | |
ejs.FilteredQuery( | |
ejs.DisMaxQuery() | |
.queries(ejs.MatchQuery('title', 'keyword')) | |
.queries(ejs.MatchQuery('content', 'keyword')) | |
.queries(ejs.MatchQuery('title', 'keyword') | |
.boost(2) | |
.type('phrase') | |
.slop(5)) | |
.queries(ejs.MatchQuery('content', 'keyword') | |
.boost(2) | |
.type('phrase') | |
.slop(20)) | |
.tieBreaker(0.1), | |
ejs.NotFilter(ejs.TypeFilter('featured')))) | |
.should( | |
ejs.ConstantScoreQuery() | |
.query(ejs.MatchQuery('keywords', 'keyword')) | |
.boost(1000000))) | |
# basic mult-select faceting | |
var colors = ejs.TermsFilter('color', ['green', 'red']), | |
shapes = ejs.TermsFilter('shape', ['square']), | |
tags = ejs.TermsFilter('tags', ['tag2', 'tag4']), | |
keywords = ejs.TermsFilter('keywords', ['keyword2', 'keyword3']) | |
ejs.Request() | |
.query(ejs.MatchAllQuery()) | |
.filter(ejs.AndFilter([colors, shapes, tags, keywords])) | |
.facet( | |
ejs.TermsFacet('shapeFacet') | |
.field('shape') | |
.allTerms(true) | |
.facetFilter(ejs.AndFilter([colors, tags, keywords]))) | |
.facet( | |
ejs.TermsFacet('colorFacet') | |
.field('color') | |
.allTerms(true) | |
.facetFilter(ejs.AndFilter([shapes, tags, keywords]))) | |
.facet( | |
ejs.TermsFacet('tagsFacet') | |
.field('tags') | |
.allTerms(true) | |
.facetFilter(ejs.AndFilter([colors, shapes, keywords]))) | |
.facet( | |
ejs.TermsFacet('keywordsFacet') | |
.field('keywords') | |
.allTerms(true) | |
.facetFilter(ejs.AndFilter([shapes, tags, keywords]))) | |
Aggregations with nested and using filters with and filter....... :)
ejs.Request().agg(
ejs.FilterAggregation('MainFilter').filter
(
ejs.AndFilter
([
ejs.TermFilter('Hospital','XXX'),
ejs.RangeFilter('log_date').from('2014-05-05 00:00:00.000').to('2014-05-11 00:00:00.000')
])
)
.agg
(
ejs.DateHistogramAggregation('DailyTime').field('log_date').interval('1d').format('E')
.agg
(
ejs.DateHistogramAggregation('HourlyTime').field('log_date').interval('1h')
.agg
(
ejs.SumAggregation('hourlyEDuration').field('EDuration')
)
.agg
(
ejs.CardinalityAggregation('hourlyECount').field('RowKEy')
)
)
.agg
(
ejs.SumAggregation('dailyEDuration').field('EDuration')
)
.agg
(
ejs.CardinalityAggregation('dailyECount').field('RowKEy')
)
)
)
These examples are extremely helpful, so I'm contributing back a query that I just wrote.
ejs.Request()
.size(0)
.query(ejs.FilteredQuery(
ejs.MatchAllQuery(),
ejs.BoolFilter()
.must(ejs.PrefixFilter('host', 'els-host'))
.must(ejs.RangeFilter('CollectionDateTime').gte('now-5m'))
))
.agg(ejs.TermsAggregation('host').field('host')
.agg(ejs.StatsAggregation('all').field('jvm.mem.heap_used_percent'))
.agg(ejs.FilterAggregation('highwater')
.filter(ejs.RangeFilter('jvm.mem.heap_used_percent').gt(68))
.agg(ejs.ValueCountAggregation('count').field('jvm.mem.heap_used_percent'))
)
)
;
This query expands to the following JSON.
{
"size": 0,
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"bool": {
"must": [
{"prefix": {"host": "els-host"}},
{"range": {"CollectionDateTime": {"gte": "now-5m"}}}
]
}
}
}
},
"aggs": {
"host": {
"terms": {"field": "host"},
"aggs": {
"all": {"stats": {"field": "jvm.mem.heap_used_percent"}},
"highwater": {
"filter": {"range": {"jvm.mem.heap_used_percent": {"gt": 68}}
},
"aggs": {
"count": {
"value_count": {"field": "jvm.mem.heap_used_percent"}
}
}
}
}
}
}
}
Good job.
Thank you. I found these useful. Some of the queries I wrote(and combined painstakingly):
var searchQuery = ejs.BoolQuery()
.should(ejs.MultiMatchQuery(["qualifiedProductName^1.5", "qualifiedProductName.shingles"])
.query('{{query_string}}')
.type('most_fields')
.minimumShouldMatch('65%')
.cutoffFrequency(0.01)
.lenient(true)
.boost(12))
.should(ejs.NestedQuery('dcs')
.query(ejs.MultiMatchQuery(["dcs.departmentName^8", "dcs.categoryName^2", "dcs.subCategoryName"])
.query('{{query_string}}')
.type('best_fields')
.minimumShouldMatch('75%')
.tieBreaker(0.3)
.fuzziness('AUTO')
.lenient(true)
.boost(4)))
.should(ejs.MultiMatchQuery([
"collectionsFeature^1.5", "colourFeature^1.5",
"materialFeature^1.5", "genderFeature^2",
"sizeDiapersFeature^2", "longDesc"
])
.query('{{query_string}}')
.type('most_fields')
.minimumShouldMatch('20%')
.fuzziness('AUTO')
.lenient(true)
.boost(2))
.should(ejs.MatchQuery('productId', '{{query_string}}'));
var topHitsAggs = ejs.Request()
.agg(ejs.TermsAggregation('variantGrouping')
.field('virtualGroupingId')
.size(0)
.order('max_score', 'desc')
.agg(ejs.TopHitsAggregation('variants').size(60))
.agg(ejs.MaxAggregation('max_score').lang('expression').script('_score')));
var facetAggs = JSON.parse(JSON.stringify(ejs.Request()
.agg(ejs.NestedAggregation('dcs')
.path('dcs')
.agg(ejs.TermsAggregation('departmentName')
.size(0)
.field('dcs.departmentName.raw')
.agg(ejs.TermsAggregation('categoryName')
.size(0)
.field('dcs.categoryName.raw')
.agg(ejs.TermsAggregation('subCategoryName')
.size(0)
.field('dcs.subCategoryName.raw')))))
.agg(ejs.HistogramAggregation('price')
.field('defaultPrice')
.minDocCount(1)
.interval(500))
.agg(ejs.StatsAggregation('priceStats')
.field('defaultPrice'))
.agg(ejs.HistogramAggregation('discountPercent')
.field('discountPercent')
.interval(10))
.agg(ejs.TermsAggregation('brand')
.field('brandName')
.size(0)
.order('_term', 'asc'))
.agg(ejs.TermsAggregation('colour')
.field('colourFeature.raw')
.size(0)
.order('_term', 'asc')).toJSON()));
# some code
return ejs.Request()
.agg(ejs.NestedAggregation('dynamicAggs')
.path('aggregationFields')
.agg(ejs.FilterAggregation('dynamicfilter')
.filter(termFilter)
.agg(ejs.TermsAggregation('key')
.field('aggregationFields.key')
.size(0)
.agg(ejs.TermsAggregation('value')
.field('aggregationFields.value')
.size(0)))));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please also write a sample to query inner/nested object fields