How to search multiple generations in Elasticsearch - ruby-on-rails

Elasticsearch's article outlines how to find objects based on a search through one generation: https://www.elastic.co/guide/en/elasticsearch/guide/current/grandparents.html
GET /company/country/_search
{
"query": {
"has_child": {
"type": "branch",
"query": {
"has_child": {
"type": "employee",
"query": {
"match": {
"hobby": "hiking"
}
}
}
}
}
}
}
What if I want to also want to query the branch for the name starting with "liverpool"? How do you modify this search to find that? I keep getting format errors and I can't seem to find information about how nest the queries online.
I've already tried this (it doesn't work):
GET /company/country/_search
{
"query": {
"has_child": {
"type": "branch",
"query": {
"has_child": {
"type": "employee",
"query": {
"match": {
"hobby": "hiking"
}
}
},
"match": {
"name": "london"
}
}
}
}
}
I got an error that said the query was malformed.
I tried bool, but it doesn't work either; I can't use has_child with a bool.

I was able to make it work with a bool query.
If I'm understanding your question correctly, this should do what you want:
POST /company/country/_search
{
"query": {
"has_child": {
"type": "branch",
"query": {
"bool": {
"must": [
{
"has_child": {
"type": "employee",
"query": {
"match": {
"hobby": "hiking"
}
}
}
},
{
"match": {
"name": "liverpool"
}
}
]
}
}
}
}
}
If you want the query to be an "OR" instead of an "AND", use "should" instead of "must".
Here is some code I set up to play around with the problem:
http://sense.qbox.io/gist/cf4babbbd25a3b7a26f3ea9ce9c1b686e37dbcb9

Related

elasticseach How to add search type in request body

I have a query where I am using search_type as
GET /test_videos/_search?search_type=dfs_query_then_fetch&explain=true
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "funny",
"fields": ["title"]
}
}
}
}
}
which works fine and gives the result I want.
I can also specify the explain in the body like this:
GET /test_claim_videos/_search?search_type=dfs_query_then_fetch
{ "explain" : true,
"query": {
"bool": {
"must": {
"multi_match": {
"query": "funny",
"fields": ["title", "asset_name", "description", "tags.name", "asset_group_name.humanized", "credit"]
}
}
}
}
}
But I want to specify the search_type as dfs_then_fetch inside the body.
If I do
GET /test_claim_videos/_search
{ "search_type" : "dfs_query_then_fetch",
"explain" : true,
"query": {
"bool": {
"must": {
"multi_match": {
"query": "funny",
"fields": ["title", "asset_name", "description", "tags.name", "asset_group_name.humanized", "credit"]
}
}
}
}
}
It raises error:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Unknown key for a VALUE_STRING in [search_type].",
"line": 1,
"col": 19
}
],
"type": "parsing_exception",
"reason": "Unknown key for a VALUE_STRING in [search_type].",
"line": 1,
"col": 19
},
"status": 400
}
Why can't I specify search type in body here and how to fix that? I need it to be in body as I am using a third party gem chewy which doesn't allow to pass URL.
Unfortunately you have no ability to do it. You can read more in documentation:
Out of the above, the search_type and the request_cache must be passed as query-string parameters.

elasticsearch count number of docs matching filter

man, this should be simple, but I can't get it.
I'm debugging, and just need to count the number of docs that match my filter.
GET my_index/_count
{
"query": {
"match_all": {},
"filter": {
"range": {
"published": {
"gte": "2018-02-19",
"format": "yyyy-MM-dd"
}
}
}
}
}
That's giving me
[match_all] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"
I'm working with version 5.6.
You could try this one:
POST my_index/_count
{
"query": {
"bool": {
"must": [{
"match_all": {}
}],
"filter": [
{ "range": {"published": { "gte": "2018-02-19", "format": "yyyy-MM-dd"} }}
]
}
}
}
You could also get the total hits when doing a simple POST my_index/_search and filter for the needed range.

Rails 4 elasticsearch using multiple search conditions, combine search feature

i'm using elasticsearch-rails for a project, there is a combine search feature, all columns are in one table. I just write a custom search function, and the search dsl didn't work, can't have any results.
def self.combine_search_filter(remark=nil, sim_card_supplier_id=nil, work_mode=nil, operator_status=nil, platform_management_status=nil, online_status=nil, sim_card_set_id=nil, iccid_from=nil, iccid_to=nil, actived_at_from=nil, actived_at_to=nil, check_in_at_from=nil, check_in_at_to=nil, device_mac_from=nil, device_mac_to=nil)
response = __elasticsearch__.search(
"size": 1000,
"query": {
"filtered": {
"filter": {
"bool": {
"filter": [
{ "term": { "sim_card_supplier_id": sim_card_supplier_id } },
{ "term": { "work_mode": work_mode } },
{ "term": { "operator_status": operator_status } },
{ "term": { "platform_management_status": platform_management_status } },
{ "term": { "online_status": online_status } },
{ "term": { "sim_card_set_id": sim_card_supplier_id } }
{ "range": { "iccid": { "from": iccid_from, "to": iccid_to }}},
{ "range": { "check_in_at": { "from": check_in_at_from, "to": check_in_at_to }}},
{ "range": { "actived_at": { "from": actived_at_from, "to": actived_at_to }}},
{ "range": { "device_mac": { "from": device_mac_from, "to": device_mac_to }}}
]
}
}
}
}
)
end
and the params maybe pass nil, how can i do that make the search dsl valid?
I would rather suggest you to use searchkick gem for rails which makes elastic search as simple as that and makes your search more intelligent and queries simple.
My bad, i just figure this out, ruby keyword function should be like this:
def self.combine_search_filter(options = {})
response = __elasticsearch__.search(
"size": 1000,
"query": {
"filtered": {
"filter": {
"bool": {
"filter": [
{ "term": { "sim_card_supplier_id": 104 } },
{ "term": { "work_mode": options[:work_mode] } },
{ "term": { "operator_status": options[:operator_status] } },
{ "term": { "platform_management_status": options[:platform_management_status] } },
{ "term": { "online_status": options[:online_status] } },
{ "term": { "sim_card_set_id": 76 } },
{ "range": { "iccid": { "from": options[:iccid_from], "to": options[:iccid_to] }}},
{ "range": { "check_in_at": { "from": options[:check_in_at_from], "to": options[:check_in_at_to] }}},
{ "range": { "actived_at": { "from": options[:actived_at_from], "to": options[:actived_at_to] }}},
{ "range": { "device_mac": { "from": options[:device_mac_from], "to": options[:device_mac_to] }}}
]
}
}
}
}
)
end
this search will have the correct result. The options[:key] may be nil will cause the wrong result, so i'm gonna delete the nil key of the dsl hash, then pass it to search()

Elasticsearch exact match only for specific fields on multi_match fields

Im trying to search only on the following fields:
name (product name)
vendor.username
vendor.name
categories_name
But the results is to wide, I want the results to be exactly what user is typed.
Example:
I type Cloth A I want the result to be exactly Cloth A not something else contain Cloth or A
Here is my attempt:
```
GET /products/_search
{
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "cloth A",
"fields": [
"name",
"vendor.name",
"vendor.username",
"categories_name"
]
}
},
"filter": [
{
"term": {
"is_available": true
}
},
{
"term": {
"is_ready": true
}
},
{
"missing": {
"field": "deleted_at"
}
}
]
}
}
}
```
How do I do that? Thanks in advance
Put this in your multi_match
"multi_match": {
"type": "best_fields"
}
This one works:
"multi_match": {
"type": "phrase"
}

First letter match in elasticsearch aggregations

I'm migrating my elasticsearch from using facets to using aggregations, and I want to create a query where the aggregations represent all the creator names that begin with a certain letter.
I've created a nested index like so:
indexes creators, type: 'nested' do
indexes :name, type: 'string', analyzer: 'caseinsensitive', index: 'not_analyzed'
end
The following query will return all the items where a creator's name begins with a "b". Great working so far.
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"nested": {
"path": "creators",
"query": {
"prefix": {
"creators.name": {
"value": "b"
}
}
}
}
}
}
},
"aggregations": {
"creators": {
"nested": {
"path": "creators"
},
"aggs": {
"name": {
"terms": {
"field": "creators.name",
"size": 100
}
}
}
}
}
}
However, the aggregations part of the query returns ALL of the aggregations for the results, including instances creator names that do not begin with a "b." For instance, if I had an item with two creators:
"creators": [
{
"name": "Beyonce"
},
{
"name": "JayZ"
}
],
The aggregation results would include both JayZ and Beyonce. Like most people, I only want Beyonce.
Try this query and see how it goes:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "creators",
"query": {
"prefix": {
"creators.name": {
"value": "b"
}
}
}
}
}
}
},
"aggregations": {
"creators": {
"nested": {
"path": "creators"
},
"aggs": {
"NAME": {
"filter": {
"prefix": {
"creators.name": "b"
}
},
"aggs": {
"name": {
"terms": {
"field": "creators.name",
"size": 100
}
}
}
}
}
}
}
}

Resources