elasticsearch


Conditional filtering in Elasticsearch


I have an index with three fields type which is an integer, coordinates which is a geopoint and title which is a string. I want to query for matches on title. Then I want to filter my results conditionally. Records that are of type 2 should be included if they fall within a 15 km radius. Those that are not of type 2 should only be included if they fall within a 7.5 km radius. The query below does not achieve this; I rather included it to give some idea of the structure of my index. This query returns matching records that are of type 2 and fall within 15 km. I'd want this query to be expanded to also include matching records that aren't of type 2 (namely, 1), but only if they fall within 7.5 km. Is this achievable in a single ES query?
Pseudocode of my filtering conditions logic:
if type == 2
filter
distance: 15 km
else
filter
distance 7.5 km
Query:
{
"query": {
"filtered": {
"query": {
"match": {
"title": "my search terms"
}
},
"filter": {
"and": [
{
"geo_distance": {
"distance": "15km",
"coordinates": [
-79.3931,
43.6709
]
}
},
{
"term": {
"type": "2"
}
}
]
}
}
}
}
ES 2.3
You can achieve what you want like this:
{
"query": {
"bool": {
"must": {
"match": {
"title": "my search terms"
}
},
"should": [
{
"bool": {
"must": [
{
"geo_distance": {
"distance": "15km",
"coordinates": [
-79.3931,
43.6709
]
}
},
{
"term": {
"type": "2"
}
}
]
}
},
{
"bool": {
"must": [
{
"geo_distance": {
"distance": "7.5km",
"coordinates": [
-79.3931,
43.6709
]
}
}
],
"must_not": {
"term": {
"type": "2"
}
}
}
}
]
}
}
}

Related Links

time difference in elasticsearch took and that calculated manually
elasticsearch comparison between fields
Find all ID where ID are not in my blacklist
How to order results by custom value and then by _score
Kibana homepage shows blank after deleting all indices
elasticsearch: update in a nested object (HTTP)
Using Phoenix to help to integrate elastic-search and Hbase. When use sqlline.py,to create table, bad happens
Shards and replicas elastic search
Elasticsearch Completion Suggester - Sort suggestions
Can _score from different queries be compared?
Difference between a “plain” terms query and a terms query using a filter
Elasticsearch: Updating a field that has been set as a document _id via mapping with a path
Return parent data with child document from Elasticsearch
Elasticsearch Giving Incorrect Result When Using “must_not” operator alongwith “must” Operator
ElasticSearch _suggest queries are case sensitive. Want them to be case insensitive
How to normalize periods in elastic search query (such that JJ Abrams == J.J Abrams)?

Categories

HOME
php
google-chrome
android-espresso
yaml
selenium-builder
tfs2015
tibco
material-components
visualforce
wagtail
graphql-js
cocotb
smooks
squid
uima
jqxgrid
multiple-tables
android-custom-view
social-tables
altium-designer
connection-refused
intel
android-cardview
onchange
math.js
amazon-cloudtrail
audit
es6-modules
appcode
formsauthenticationticket
code-rally
identify
variable-variables
llvm-ir
rdp
raytracing
dagger
jqgrid-asp.net
apache-toree
reverse-dns
viewmodel
deployd
beeline
lightning-workbench
android-mediarecorder
patternlab.io
glimpse
instafeedjs
perl-data-structures
pbx
deviare
coovachilli
ajaxmin
powerpoint-2013
strpos
adjacency-list
ioctl
enunciate
phpquery
html.actionlink
css-paged-media
mobilefirst-server
rfc5545
sql-processor
famo.us
symja
dnsjava
dc
utf8-decode
dmake
inputbox
oracle-adf-mobile
osascript
windows-messages
jchartfx
lync-2010
icenium
linkbutton
lynx
multiscaleimage
xcode4.5
gitx
story
wndproc
first-class
prefuse
quartz-core
simile
jmenu
django-pagination
uninstaller
3270
eai

Resources

Encrypt Message