How join parent and child in elasticsearch?
For example my mapping:
{
"street":{
"properties":{
"street_name":{
"type":"string"
}
}
}
}
and
{
"address":{
"_parent":{
"type":"street"
},
"properties":{
"house_number":{
"type":"string"
}
}
}
}
Data:
{"_type":"street","_id":"AUrLQH9ZcB6int_hskH3","_source":{"street_name":"Street"}}
{"_type":"address","_id":"AUrLQH_XcB6int_hskH4","_source":{"house_number":"10"}}
How i can get similar result in one query:
{
"_type":"address",
"_id":"AUrLQH_XcB6int_hskH4",
"house_number":"10",
"street_name":"Street"
}
Of course, for a single element, i can join types in application, but what to do with large lists?
Thanks!
Maybe something like this, don't think there is a way of grouping them nicely:
GET /postal_codes/street/_search?search_type=count
{
"aggs": {
"street": {
"terms": {
"field": "street_name"
},
"aggs": {
"addresses": {
"children": {
"type": "address"
},
"aggs": {
"number": {
"terms": {
"field": "address.house_number"
}
}
}
}
}
}
}
}
Related
I'm new to API Connect, and I haven't been able to find the correct mapping to pass from an array of objects to an object, evaluating its content.
I explain:
I have as input a json like this:
{
"methodCall": {
"methodName": {
"$": "ThisIsTheMethodName"
},
"params": {
"param": {
"value": {
"array": {
"data": {
"value": {
"struct": {
"member": [
{
"name": {
"$": "message"
},
"value": {
"string": {
"$": "Some text to send to client"
}
}
},
{
"name": {
"$": "phone"
},
"value": {
"string": {
"$": "9876543120124"
}
}
},
{
"name": {
"$": "date"
},
"value": {
"string": {}
}
},
{
"name": {
"$": "appid"
},
"value": {
"string": {
"$": "Application Identificator"
}
}
},
{
"name": {
"$": "costCenter"
},
"value": {
"string": {
"$": "102030"
}
}
},
{
"name": {
"$": "filled"
},
"value": {
"string": {
"$": "filledString"
}
}
}
]
}
}
}
}
}
}
}
}
}
and I need to generate this json output from the mapping:
{
"phoneNumberSMS":"983849780",
"message":"Some text to send to client",
"date": "2022-10-04T15:30:00",
"appId":"Application Identificator",
"costCenter":"102030",
"filled":"filledString" }
I have tried with the following configuration, but without success:
On the YAML
actions:
- set: output.phoneNumberSMS
foreach: input.methodCall.params.param.value.array.data.value.struct.member.value.string
from:
- input.methodCall.params.param.value.array.data.value.struct.member.name.$
- input.methodCall.params.param.value.array.data.value.struct.member.value.string.$
values: |-
var retValue1 = '';
if($(input.methodCall.params.param.value.array.data.value.struct.member.name.$) == 'phone'){
retValue1=input.methodCall.params.param.value.array.data.value.struct.member.value.string.$;
}
retValue1;
I appreciate your help !!
I solve this in two phases of mapping:
Create an array called members, where each node is of type member, which has name and value properties.
This 'members' array is the receiver of the data coming from the request.
In the second phase of the mapping, I took the output variable from the previous mapping (of type members) and assigned it to message.body.
This with the aim of getting rid of the field names with a dollar symbol ($), so the mapping will not give any error for not recognizing it.
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()
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"
}
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
}
}
}
}
}
}
}
}
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