I have two indexes, businesses
and categories
. Each have different mappings, one of those differences being that businesses
has a geofield (lat/lng) associated with it. I would like to perform a query where a user could perform an autocomplete search that would span the two indexes (think Yelp.com). Additionally, the user's location would be provided so that only businesses
in some x
distance would appear. However, any categories
that match the search should appear, as it doesn't matter where a user is located when returning categories
. Because I do not associate categories
with a geofield, I'm getting an error that the geofield property can't be found, and rightfully so (it works when I just query businesses
). Is there a way to structure my query so that one statement only looks at one index, and one statement only looks at another index? Or do I need to give the categories some "dummy" geofield that would be ignored by adding a type
property and using an or
operator for "type":"category"
? I.e., "matches the geoquery OR is type:category
".
Asked
Active
Viewed 98 times
0

mjoyce91
- 316
- 2
- 19
1 Answers
1
If I understand what you want to do, it can be rephrased as a boolean expression, like:
("index == businesses" AND "<geoquery> is OK") OR ("index == categories" AND "<categoryquery> is OK")
Here are a few hints to achieve this query :
- An OR query can be defined in elasticsearch as a "bool" query with 2 or more "should" clauses and "minimum_should_match" set to 1
- An AND query can be defined in elasticsearch as a "bool" query with 2 or more "must" clauses
- You can check the index in your 2 subqueries using the "_index" field:
- You will have to define the geopoint field in the mapping of "categories" index (not necessarily in the json documents of "categories" index)
You did not provide the geoquery and categoryquery, so i'll let them as placeholders, you will just have to replace them.
You should try something like this (elasticsearch v5.2.2 syntax, should work in elasticsearch v2.0 too) :
GET businesses,categories/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [{
"bool": {
"must": [
{
"term": {
"_index": "businesses"
}
},
{
"<your_geoip_query>": {
<your_geoip_query_params>
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"_index": "categories"
}
},
{
"<your_category_query>": {
<your_category_query_params>
}
}
]
}
}
]
}
}
}

Damien Ferey
- 99
- 6
-
Thanks for the reply! Unfortunately because my `categories` index doesn't have a geo_point field, I receive this error: `..."reason": "failed to find geo_point field [field_geo:latlon]", "index": "categoriess"...` Will I need to add a dummy field? – mjoyce91 Apr 22 '17 at 13:56
-
1unfortunately, "filtered query" doesn't help, I tried and the failure section is still present. I think that yes, you will have to add a dummy field in the other index, but only at mapping level of the "categories" index, not in the object sources. (I tested on my elastic 2.0, there are no more errors after doing this) – Damien Ferey Apr 22 '17 at 16:40
-
I added a dummy `geo_point` field and it works! If you update your answer to note that a dummy field must be added, I'll accept it (just for any one who might come across this post). – mjoyce91 Apr 24 '17 at 12:47
-
1Done ;-) (4th hint) – Damien Ferey Apr 24 '17 at 13:48