Elasticsearch how to get objects from a nested structure - elasticsearch

Here is the structure of my document:
{
'parent_list': [
{
'child_list': [
{
'name': 'A',
'score': 0.86
},
{
'name': 'B',
'score': 0.92
},
{
'name': 'C',
'score': 0.77
}
]
},
{
'child_list': [
{
'name': 'B',
'score': 0.41
},
{
'name': 'D',
'score': 0.66
}
]
},
]
}
What I want is to get the object with maximum score property from each child_list, i.e. From the document above i would like to get something like this:
[
{
'name': 'B',
'score': 0.92
},
{
'name': 'D',
'score': 0.66
}
]
Or, it would also be plenty to just receive the name property of these objects.
I use nested mapping type for both parent_list and child_list, name is text, and score is float.
So far i have tried using nested aggregation/queries and I am able to get max score from each of the child_list lists, so that would be: 0.92, 0.66. But i cannot figure out how to get the corresponding name property. Any help is welcome.

Related

Is there any way no to return a document if the word present in one list

I have document below
[
{'id':1, 'name': 'sachin messi', 'description': 'football#football.com', 'type': 'football', 'var':'sports'},
{'id':2, 'name': 'lionel messi', 'description': 'messi#fifa.com','type': 'soccer','var':'sports'},
{'id':3, 'name': 'sachin tendulkar', 'description': 'was', 'type': 'cricket', 'var':'sports'}
]
I have one list var.txt contain
sachin
I have to avoid sachin from the document
DSL query is below
{
"query": {
"bool": {
"must": [
{
"terms": {
"var.keyword": [
"sports"
]
}
},
{
"query_string": {
"query": "sach* OR mess*",
"fields": [
"name^1024",
"description^32"
]
}
}
]
}
}
}
My Output is below
{'took': 128,
'timed_out': False,
'_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
'hits': {'total': {'value': 2, 'relation': 'eq'},
'max_score': 1201.876,
'hits': [{'_index': 'newtestplayer',
'_type': '_doc',
'_id': '3',
'_score': 1201.876,
'_source': {'id': 3,
'name': 'sachin tendulkar',
'description': 'was',
'type': 'cricket',
'var': 'sports'}},
{'_index': 'newtestplayer',
'_type': '_doc',
'_id': '2',
'_score': 929.40845,
'_source': {'id': 2,
'name': 'lionel messi',
'description': 'messi#fifa.com',
'type': 'soccer',
'var': 'sports'}}]}}
You can see my query "sach* OR mess*"
My Expected should contain only id:1 and id:2 and not contain id:3
This is working if my setting is added as sachin as stopwords and I need to search like "sachin OR mess*"
My requirement is my output document should not appear if word contain sachin even if i m searching *sach*
so this cannot done from stop words if my document is contain 'sachi' and i m searching for sach* then 'sachin' should not come and sachi document should come
You can use must_not query inside bool from Elasticsearch.
{
"query": {
"bool": {
"must": [
{
"terms": {
"var.keyword": [
"sports"
]
}
},
{
"query_string": {
"query": "sach* OR mess*",
"fields": [
"name^1024",
"description^32"
]
}
}
],
"must_not": [
{
"query_string": {
"query": "sachin",
"fields": [
"name",
"description"
]
}
}
]
}
}
}

How to write ES search queries for complex documents structure?

I have the following documents and structure:
Document 1:
{
'id': '1',
'data': {
'parents': [],
'people': []
}
Document 2:
{
'id': '2',
'data': {
'parents': ["one", "two"],
'people': [{'relationship': 'boss', 'ids': ['1']}, {'relationship': 'friends', 'ids': ['1']}]
}
Document 3:
{
'id': '3',
'data': {
'parents': ["one", "two"],
'people': [{'relationship': 'boss', 'ids': ['1', '2']}, {'relationship': 'friends', 'ids': ['1', '2']}]
}
}
I want to delete a document given an id and then delete any relationships with document in other documents.
So given id = '1', document 1 is deleted, and document 2 and 3's relationship entires that include '1' are updated to not include '1' anymore; so after document 1 is deleted, we have the following:
Document 2:
{
'id': '2',
'data': {
'parents': ["one", "two"],
'people': []
}
Document 3:
{
'id': '3',
'data': {
'parents': ["one", "two"],
'people': [{'relationship': 'boss', 'ids': ['2']}, {'relationship': 'friends', 'ids': ['2']}]
}
}
I am struggling writing the ES queries because the document's structure is a complex one;
What I have tried so far are the following:
{ 'query': { 'match': { 'id.keyword': '1' } } }
{ 'terms': {data.people.ids.keyword: ['1'], 'minimum_should_match': 1 } }
but they do not work, nor do they throw an error.

RadDataForm Multiple select in nativescript-vue

I have this TKEntityProperty:
<TKEntityProperty v-tkDataFormProperty name="groups" displayName="Groups" index="2" :valuesProvider="retrieveGroups">
and this gets values from below object:
retrieveGroups:[
{key: "1", "label": "Group 1"},
{key: "2", "label": "Group 2"},
{key: "3", "label": "Group 3"}
]
but it does not multi select. I want to select multiple elements.
Is there another type of editor available ?
As #Manoj suggested, you should use AutoCompleteInline
Here is an example, it is available at Nativescript github page
data() {
return {
title: description,
booking: new Booking(),
bookingMetadata: {
'isReadOnly': false,
'commitMode': DataFormCommitMode.Immediate,
'validationMode': DataFormValidationMode.Immediate,
'propertyAnnotations': [{
'name': 'from',
'displayName': 'From:',
'index': 0,
'editor': DataFormEditorType.AutoCompleteInline,
'editorParams': {
'autoCompleteDisplayMode': AutoCompleteDisplayMode.Tokens
},
'valuesProvider': fromProviders,
},
{
'name': 'to',
'displayName': 'To:',
'index': 1,
'editor': DataFormEditorType.AutoCompleteInline,
'editorParams': {
'autoCompleteDisplayMode': AutoCompleteDisplayMode.Plain
},
'valuesProvider': ['New York', 'Washington', 'Los Angeles'],
},
]
}
};
},

Laravel collection: return only relationship and key

I have a collection that resembles this:
$a = Model::with(['sub' => function($q) {
$q->select('id', 'name')
}])->get();
This returns the following collection:
{
0: {
id: 0001,
name: "item 1",
type: "type a"
'sub' [
{
'id': 10001,
'name': "sub Item 1"
},
{
'id': 10002,
'name': "sub Item 2"
}
]
},
1: {
id: 0002,
name: "item 2",
type: "type a"
'sub' [
{
'id': 11001,
'name': "sub Item 4"
},
{
'id': 11002,
'name': "sub Item 5"
}
]
}
What I am trying to do is key the parent items by their ids and only return the relationship. For example
{
0001: {
'sub' [
{
'id': 10001,
'name': "sub Item 1"
},
{
'id': 10002,
'name': "sub Item 2"
}
]
},
0002: {
'sub' [
{
'id': 11001,
'name': "sub Item 4"
},
{
'id': 11002,
'name': "sub Item 5"
}
]
}
I cannot seem to get this to work. I have tried many variations including:
$a = Model::with(['sub' => function($q) {
$q->select('id', 'name')
}])->pluck('sub', 'id');
This doesn't work as 'Pluck' is obviously looking for a a property of the parent model with the name of 'sub' which doesn't exit. Is there a way to achieve this?
Thanks
You were almost there. You will need to do ->get() before the pluck().
$a = Model::with([
'sub' => function ($q) {
$q->select('id', 'name');
},
])->get()->pluck('sub', 'id');
The pluck() used in your example will be the query builder version of pluck rather than the collection version.
use keyBy to use your pk as array index.
https://laravel.com/docs/5.4/collections#method-keyby
However ignoring other fields you probably would need each and filter. Wouldn't it be easier to select Sub::where(...) and then use collection groupBy on the parent_id: https://laravel.com/docs/5.4/collections#method-groupby
So something like Sub::where(...)->get()->groupBy('parent_id')

Datetime and numbers localization in adreportstats async query

Is there any way to set datetime and numbers format based on locale info in adreportstats asynchronous queries? I've tried to fetch data with request parameter "locale" but it fails.
UPD:
There is no code for this issue. I just want to get in response localized json data. Here is response example:
{
'data': [
{
'adgroup_id': 1,
'country': 'US',
'gender': 'female',
'date_start': 2013-09-01,
'date_stop': 2013-09-02,
'reach': 2000,
'ctr': 0.42333333333333
},
{
'adgroup_id': 2,
'country': 'UK',
'gender': 'male',
'date_start': 2013-09-01,
'date_stop': 2013-09-02,
'reach': 1500,
'ctr': 0.33333333333333
},
...
]}
I want to get it for "de_DE" locale:
{
'data': [
{
'adgroup_id': 1,
'country': 'US',
'gender': 'female',
'date_start': 01.09.2013, <------ de_DE localized date
'date_stop': 02.09.2013,
'reach': 2000,
'ctr': 0,42333333333333 <------ de_DE localized number
},
{
'adgroup_id': 2,
'country': 'UK',
'gender': 'male',
'date_start': 01.09.2013,
'date_stop': 02.09.2013,
'reach': 1500,
'ctr': 0,33333333333333
},
...
]}
By design: "The Ads API does not support the "locale" parameter. The supported parameters for Ad Report Stats are documented here: https://developers.facebook.com/docs/reference/ads-api/adreportstats/"

Resources