DSL query to find the elements from array of dictionaries - elasticsearch

I have a index in elastic search called professor
If for cross field i need "AND" condition
for same field array i need to OR condition
I need to search BusinessArea which is Research or Accounting this is array of fields(OR) statement
AND
I need to search Role is Developer or Tester condition this is array of fields(OR) statement
AND
I need to search Location is NY(&) condition
test = [{ 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting' }, { 'id': '3', 'name': 'Accounting' } ],'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '2', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Engineering' } ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }] }]
Query is below,3rd one got it, How to add 1 and 2
content_search = es.search(index="professor", body={
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{
"term": {
"Location.keyword": "NY"
}
}
]
}
}
})
content_search ['hits']['hits']

I need to search Location is NY(&) condition
I have not added this condition, since there is no field named Location in your sample data
Try out this below query:
Search Query:
{
"query": {
"bool": {
"must": [
{
"terms": {
"BusinessArea.name.keyword": [
"Research",
"Accounting"
]
}
},
{
"terms": {
"Role.name.keyword": [
"Developer",
"Tester"
]
}
}
]
}
}
}
Search Result:
"hits": [
{
"_index": "stof_64386038",
"_type": "_doc",
"_id": "2",
"_score": 2.0,
"_source": {
"id": "2",
"name": "Group1",
"BusinessArea": [
{
"id": "14",
"name": "Research"
},
{
"id": "3",
"name": "Accounting"
}
],
"Role": [
{
"id": "5032",
"name": "Tester"
},
{
"id": "5033",
"name": "Developer"
}
],
"Designation": [
{
"id": "16",
"name": "L1"
},
{
"id": "20",
"name": "L2"
},
{
"id": "25",
"name": "L2"
}
]
}
},
{
"_index": "stof_64386038",
"_type": "_doc",
"_id": "3",
"_score": 2.0,
"_source": {
"id": "1",
"name": "Group1",
"BusinessArea": [
{
"id": "14",
"name": "Research"
},
{
"id": "3",
"name": "Engineering"
}
],
"Role": [
{
"id": "5032",
"name": "Developer"
},
{
"id": "5033",
"name": "Developer"
}
],
"Designation": [
{
"id": "16",
"name": "L1"
},
{
"id": "20",
"name": "L2"
},
{
"id": "25",
"name": "L2"
}
]
}
}
]

You can use bool query like this:
content_search = es.search(index="professor", body={
"query": {
"bool": {
"must": [
{
"terms":
{
"BusinessArea.name.keyword": ["Research","Accounting"]
}
},
{
"terms":
{
"Role.name.keyword": ["Developer","Tested"]
}
}
]
},
"filter": [
{
"term": {
"Location.keyword": "NY"
}
}
]
}
})

Related

filtering elastic search results by fields and values

I have a sample data that includes data from univerities, schools and dormitories.
I want to use this data in Elasticsearch. I send the body as follows when making filteration:
Sample Data:
[
{
name: 'Maisha school of sience',
school_type: 'public',
location: 'A',
},
{
name: 'Maisha elementry school',
school_type: 'private',
location: 'B',
},
{
name: 'istanbul university',
university_type: 'public',
location: 'C',
},
{
name: 'Maisha university',
university_type: 'private',
location: 'D',
},
{
name: 'kbb center of Maisha',
dormitory_type: 'public',
location: 'E',
},
{
name: 'high Maisha dorm',
dormitory_type: 'private',
location: 'F',
},
]
My Query:
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*Maisha*",
"fields": [
"name"
]
}
},
{
"match": {
"school_type": "public"
}
}
]
}
}
Result:
{
name: 'Maisha school of sience',
school_type: 'public',
location: 'A',
}
This result actually makes sence because it gives the exact result but dormitory and university data are not shown which is the main problem. In other words, I only want the school_type to be public but why it filters out and not showing university and dormitory data?
You are almost correct, you just need to change must in bool to should. I just indexed your sample data and using below query.
Sample documents
{
"name": "Maisha school of sience", // doc-1
"school_type": "public",
"location": "A"
}
{
"name": "istanbul university", // doc-2
"university_type": "public",
"location": "C"
}
{
"name": "Maisha university", // so on
"university_type": "private",
"location": "D"
}
{
"name": "kbb center of Maisha",
"dormitory_type": "public",
"location": "E"
}
{
"name": "high Maisha dorm",
"dormitory_type": "private",
"location": "F"
}
{
"query": {
"bool": {
"should": [. ->> notice should clause here
{
"query_string": {
"query": "*Maisha*",
"fields": [
"name"
]
}
},
{
"match": {
"school_type": "public"
}
}
]
}
}
}
it returns below result, hope you are looking for the same output.
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.287682,
"hits": [
{
"_index": "71296703",
"_id": "1",
"_score": 1.287682,
"_source": {
"name": "Maisha school of sience",
"school_type": "public",
"location": "A"
}
},
{
"_index": "71296703",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "Maisha university",
"university_type": "private",
"location": "D"
}
},
{
"_index": "71296703",
"_id": "4",
"_score": 1.0,
"_source": {
"name": "kbb center of Maisha",
"dormitory_type": "public",
"location": "E"
}
},
{
"_index": "71296703",
"_id": "5",
"_score": 1.0,
"_source": {
"name": "high Maisha dorm",
"dormitory_type": "private",
"location": "F"
}
}
]
}
}

Elasticsearch complex aggregation One nested value minus second nested value and group by third nested value

I'm really confused.
I created a nested structure with three types of data inside, addfields is nested data type with properties:
id type integer
value type text fielddata true
value_numeric type double
Data inside elastic looks like:
{
"_index": "item",
"_type": "_doc",
"_source": {
"id": 1,
"addfields": [
{
"id": 1,
"value_numeric": 10
},
{
"id": 2,
"value_numeric": 10
},
{
"id": 3,
"value": "Daniel"
},
]
},
}
------------------------------------------------------------------------------------
{
"_index": "item",
"_type": "_doc",
"_source": {
"id": 2,
"addfields": [
{
"id": 1,
"value_numeric": 150
},
{
"id": 2,
"value_numeric": 100
},
{
"id": 3,
"value": "Daniel"
},
]
},
}
------------------------------------------------------------------------------------
{
"_index": "item",
"_type": "_doc",
"_source": {
"id": 2,
"addfields": [
{
"id": 1,
"value_numeric": 250
},
{
"id": 2,
"value_numeric": 200
},
{
"id": 3,
"value": "Richard"
},
]
},
}
I want to aggregate this data like:
addfields[1] - addfields[2] and group them by addfields[3]
I tried to use the bucket_script as following, but things didn't go well.
{
"size": 0,
"aggs": {
'nested' => [
'path' => 'addfields',
],
"addfields": {
"aggs": {
"leftfield": {
"filter": {
"bool": {
"must": {
{
"match": {"addfields.id": 1}
}
}
}
},
"aggs": {
"values": {
"terms": {
"field": "value_numeric",
"size": 9999
}
}
}
},
"rightfield": {
"filter": {
"bool": {
"must": {
{
"match": {"addfields.id": 2}
}
}
}
},
"aggs": {
"values": {
"terms": {
"field": "value_numeric",
"size": 9999
}
},
"calculate": {
"bucket_script": {
"buckets_path": {
"left": "leftfield",
"right": "rightfield"
},
"script": {
"source": "params.leftfield - params.rightfield"
}
}
}
}
}
}
}

Jolt Transform to Array with Parent Attributes

I am using NiFi Jolt Processor to transform some JSON data.
My JSON field genre contains shared attributes and an array that contains the list of actual genre names.
I needed to transform the "genre" attribute to become an array of "genres" containing the list of the common attributes and the different genre names.
I have the following input JSON:
{
"programIdentifier": "9184663",
"programInstance": {
"genre": {
"source": "GN",
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"genres": [
"Miniseries",
"Drama"
]
}
}
}
I have tried the following spec:
[{
"operation": "shift",
"spec": {
"programIdentifier": ".&",,
"genre": {
"source": "genres[].source.value",
"locked": "genres[].locked",
"lastModifiedDate": "genres[].lastModifiedDate",
"lastModifiedBy": "genres[].lastModifiedBy",
"genres": {
"*": "genres[&0].name"
}
}
}]
This my expected output:
{
"programIdentifier": "9184663",
"programInstance": {
"genres": [
{
"source": {
value: "GN"
}
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"name": "Miniseries"
},
{
"source": {
value: "GN"
}
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"name": "Drama"
}
]
}
}
But it's coming out as:
{
"programIdentifier": "9184663",
"programInstance": {
"genres": [
{
"source": {
"value": "GN"
},
"name": "Miniseries"
}, {
"locked": false,
"name": "Drama"
}, {
"lastModifiedDate": 1527505462094
}, {
"lastModifiedBy": "Some Service"
}],
}
}
Is it what you want to achieve?
[
{
"operation": "shift",
"spec": {
"programIdentifier": ".&",
"programInstance": {
"genre": {
"genres": {
"*": {
"#2": {
"source": "programInstance.genres[&2].source[]",
"locked": "programInstance.genres[&2].locked",
"lastModifiedDate": "programInstance.genres[&2].lastModifiedDate",
"lastModifiedBy": "programInstance.genres[&2].lastModifiedBy"
},
"#": "programInstance.genres[&1].name"
}
}
}
}
}
}
]

From the below json data how can I print the tlds using foreach loop in laravel

Find below my json data and suggest me how to print the tld using foreach loop in laravel. Suggest me how to declare this array structure using foreach loop.
{
"result": "success",
"currency": {
"id": "1",
"code": "INR",
"prefix": " \u20b9",
"suffix": "INR",
"format": "1",
"rate": "1.00000"
},
"pricing": {
"in": {
"categories": [
"ccTLD",
"Geography"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "704.39"
},
"transfer": {
"1": "704.39"
},
"renew": {
"1": "704.39"
}
},
"in.net": {
"categories": [
"Other"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "547.69"
},
"transfer": {
"1": "547.69"
},
"renew": {
"1": "547.69"
}
},
"info": {
"categories": [
"gTLD",
"Popular"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "861.10"
},
"transfer": {
"1": "861.10"
},
"renew": {
"1": "861.10"
}
},
"org": {
"categories": [
"gTLD",
"Popular"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "939.45"
},
"transfer": {
"1": "939.45"
},
"renew": {
"1": "939.45"
}
},
"com": {
"categories": [
"gTLD",
"Popular"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "740.43"
},
"transfer": {
"1": "740.43"
},
"renew": {
"1": "740.43"
}
},
"net": {
"categories": [
"gTLD",
"Popular"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "829.76"
},
"transfer": {
"1": "829.76"
},
"renew": {
"1": "829.76"
}
},
"biz": {
"categories": [
"gTLD",
"Popular"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "",
"register": {
"1": "878.33"
},
"transfer": {
"1": "878.33"
},
"renew": {
"1": "878.33"
}
}
}
}
From the above data I wanted to display the tlds such as in,in.net,org,info,com,net,biz.
Use this:
$tlds = array_keys(json_decode($json, true)['pricing']);

datatables number of results from JSON

I have a pretty simple question on datatables for which I couldn't find an answer.
I'm using Spring MVC to generate a JSON, which has the following format:
{
"content": [
{
"id": 1,
"firstName": "\u00d6sten",
"lastName": "Dixon",
"email": "hdixon0#forbes.com",
"country": "Belarus",
"ipAddress": "196.228.168.184",
"creditCard": "060479202511002980",
"company": "Trunyx",
"timezone": "Atlantic\/South_Georgia",
"iban": "LV55 ECXH IGFF GSEW YHMU G",
"latitude": "52.644",
"longitude": "25.2027",
"race": "Blackfeet"
},
{
"id": 2,
"firstName": "Agn\u00e8s",
"lastName": "Perry",
"email": "wperry1#blogspot.com",
"country": null,
"ipAddress": "75.63.229.226",
"creditCard": "5191970963453131",
"company": "Tanoodle",
"timezone": "Asia\/Kabul",
"iban": "TN75 8279 0633 7374 0209 2905",
"latitude": "11.97444",
"longitude": "-86.09417",
"race": null
},
// more
],
"totalPages": 51,
"last": false,
"totalElements": 503,
"size": 10,
"number": 0,
"sort": null,
"numberOfElements": 10,
"first": true
}
I'd like to display the result using datatables, however it looks like it's expecting a "iTotalRecords" for example, where my JSON has a "totalElements"
How can I specify that mapping?
$(document).ready(function () {
$('#datatable').DataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "members",
"sAjaxDataProp" : 'content',
"columns": [
{ "data": "id" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "country" },
{ "data": "ipAddress" },
{ "data": "creditCard" },
{ "data": "company" },
{ "data": "timezone" },
{ "data": "iban" },
{ "data": "latitude" },
{ "data": "longitude" },
{ "data": "race" },
]
});

Resources