How to get one element from array of objects in elasticsearch - elasticsearch

I have a products index which has an offers field. Offers is an array of objects.
I want to return one offer by seller_id in an array or in a new field.
Input:
with seller_id=5
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offers":[
{
"product_id":"138",
"seller_id":"1",
"sale_status":"2",
"external_id":"1267631",
"store_id":"2",
"qty":"44",
"storage_address":"",
"base_price":"312.84",
"updated_at":"2022-11-15T10:42:49.000000Z",
"created_at":"2022-11-15T10:42:49.000000Z",
"id":74
},
{
"product_id":"138",
"seller_id":"2",
"sale_status":"1",
"external_id":"2795841",
"store_id":"2",
"qty":"1",
"storage_address":"",
"base_price":"1812.3",
"updated_at":"2022-11-15T10:44:50.000000Z",
"created_at":"2022-11-15T10:44:50.000000Z",
"id":76
},
{
"product_id":"138",
"seller_id":"3",
"sale_status":"1",
"external_id":"32219",
"store_id":"1",
"qty":"32",
"storage_address":"",
"base_price":"1556.25",
"updated_at":"2022-11-15T10:50:16.000000Z",
"created_at":"2022-11-15T10:50:16.000000Z",
"id":77
},
{
"product_id":"138",
"seller_id":"4",
"sale_status":"1",
"external_id":"967427",
"store_id":"1",
"qty":"35",
"storage_address":"",
"base_price":"137.62",
"updated_at":"2022-11-15T10:50:18.000000Z",
"created_at":"2022-11-15T10:50:18.000000Z",
"id":78
},
{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
},
{
"product_id":"138",
"seller_id":"6",
"sale_status":"1",
"external_id":"522912",
"store_id":"1",
"qty":"61",
"storage_address":"",
"base_price":"306.39",
"updated_at":"2022-11-15T10:50:20.000000Z",
"created_at":"2022-11-15T10:50:20.000000Z",
"id":80
}
]
}
}
Expected:
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offers":[
{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
}
]
}
}
Or expected:
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offer":{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
}
}
}
Thanks for help!

If the offers field is nested type you can to use inner hits to get only object match in list.
The object you expected will in "inner_hits" response.
Query
GET idx_nested/_search?filter_path=hits.hits
{
"query": {
"nested": {
"path": "offers",
"query": {
"match": {
"offers.seller_id": "5"
}
},
"inner_hits": {}
}
}
}
Response:
{
"hits": {
"hits": [
{
"_index": "idx_nested",
"_id": "kYyYf4QBgXg8h_rctd1z",
"_score": 1.540445,
"_source": {
"is_adult": false,
"status_id": 3,
"allow_publish": false,
"name": "Consequuntur expedita sit perferendis est.",
"category_id": 816,
"brand_id": 363,
"description": "Nec.",
"type": 3,
"vendor_code": "4968258909901",
"barcode": "98735976",
"code": "consequuntur-expedita-sit-perferendis-est",
"updated_at": "2022-11-15T10:42:33.000000Z",
"created_at": "2022-11-15T10:42:33.000000Z",
"id": 138,
"offers": [
{
"product_id": "138",
"seller_id": "1",
"sale_status": "2",
"external_id": "1267631",
"store_id": "2",
"qty": "44",
"storage_address": "",
"base_price": "312.84",
"updated_at": "2022-11-15T10:42:49.000000Z",
"created_at": "2022-11-15T10:42:49.000000Z",
"id": 74
},
{
"product_id": "138",
"seller_id": "2",
"sale_status": "1",
"external_id": "2795841",
"store_id": "2",
"qty": "1",
"storage_address": "",
"base_price": "1812.3",
"updated_at": "2022-11-15T10:44:50.000000Z",
"created_at": "2022-11-15T10:44:50.000000Z",
"id": 76
},
{
"product_id": "138",
"seller_id": "3",
"sale_status": "1",
"external_id": "32219",
"store_id": "1",
"qty": "32",
"storage_address": "",
"base_price": "1556.25",
"updated_at": "2022-11-15T10:50:16.000000Z",
"created_at": "2022-11-15T10:50:16.000000Z",
"id": 77
},
{
"product_id": "138",
"seller_id": "4",
"sale_status": "1",
"external_id": "967427",
"store_id": "1",
"qty": "35",
"storage_address": "",
"base_price": "137.62",
"updated_at": "2022-11-15T10:50:18.000000Z",
"created_at": "2022-11-15T10:50:18.000000Z",
"id": 78
},
{
"product_id": "138",
"seller_id": "5",
"sale_status": "2",
"external_id": "209466",
"store_id": "1",
"qty": "45",
"storage_address": "",
"base_price": "187.03",
"updated_at": "2022-11-15T10:50:19.000000Z",
"created_at": "2022-11-15T10:50:19.000000Z",
"id": 79
},
{
"product_id": "138",
"seller_id": "6",
"sale_status": "1",
"external_id": "522912",
"store_id": "1",
"qty": "61",
"storage_address": "",
"base_price": "306.39",
"updated_at": "2022-11-15T10:50:20.000000Z",
"created_at": "2022-11-15T10:50:20.000000Z",
"id": 80
}
]
},
"inner_hits": {
"offers": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "idx_nested",
"_id": "kYyYf4QBgXg8h_rctd1z",
"_nested": {
"field": "offers",
"offset": 4
},
"_score": 1.540445,
"_source": {
"store_id": "1",
"updated_at": "2022-11-15T10:50:19.000000Z",
"storage_address": "",
"product_id": "138",
"qty": "45",
"base_price": "187.03",
"sale_status": "2",
"created_at": "2022-11-15T10:50:19.000000Z",
"external_id": "209466",
"id": 79,
"seller_id": "5"
}
}
]
}
}
}
}
]
}
}

Related

How to insert header tags to API Json

Hello im making a GET api in laravel that shows values from database and im trying to make it that the returned json result to have a header tag an example is this public api
https://dummy.restapiexample.com/api/v1/employees
As you can see this api shows the status:success and then data: , this is my code:
function getDevice()
{
return Client::all();
}
And this is the result i get in postman:
data [ //here to have a header
{
"id": 1,
"name": "Mr. Omari Schaefer DVM",
"email": "kulas.julian#example.net",
"phone_number": "0697830800",
"age": "29",
"salary": "150",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 2,
"name": "Hattie Brakus",
"email": "deckow.coby#example.com",
"phone_number": "0697830800",
"age": "30",
"salary": "565",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 3,
"name": "Sabrina Rosenbaum",
"email": "kautzer.madison#example.net",
"phone_number": "0697830800",
"age": "31",
"salary": "254",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 4,
"name": "Ms. Fiona Fritsch",
"email": "juwan.damore#example.net",
"phone_number": "0697830800",
"age": "32",
"salary": "029",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 5,
"name": "Michael Dooley",
"email": "hayes.reinhold#example.org",
"phone_number": "0697830800",
"age": "33",
"salary": "265",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 6,
"name": "Prof. Kelley Koepp Jr.",
"email": "kutch.jessie#example.org",
"phone_number": "0697830800",
"age": "34",
"salary": "688",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 7,
"name": "Herminia McClure PhD",
"email": "ywillms#example.com",
"phone_number": "0697830800",
"age": "35",
"salary": "103",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 8,
"name": "Morton Considine",
"email": "lesly.pfeffer#example.com",
"phone_number": "0697830800",
"age": "36",
"salary": "804",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 9,
"name": "Katlyn Muller",
"email": "peter93#example.net",
"phone_number": "0697830800",
"age": "37",
"salary": "695",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 10,
"name": "Wilber Stehr",
"email": "bmckenzie#example.net",
"phone_number": "0697830800",
"age": "38",
"salary": "941",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 11,
"name": "Test User",
"email": "test#example.com",
"phone_number": "0697830800",
"age": "60",
"salary": "448",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
}
]
What im trying to do is:
[
{
"id": 1,
"name": "Mr. Omari Schaefer DVM",
"email": "kulas.julian#example.net",
"phone_number": "0697830800",
"age": "29",
"salary": "150",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 2,
"name": "Hattie Brakus",
"email": "deckow.coby#example.com",
"phone_number": "0697830800",
"age": "30",
"salary": "565",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 3,
"name": "Sabrina Rosenbaum",
"email": "kautzer.madison#example.net",
"phone_number": "0697830800",
"age": "31",
"salary": "254",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 4,
"name": "Ms. Fiona Fritsch",
"email": "juwan.damore#example.net",
"phone_number": "0697830800",
"age": "32",
"salary": "029",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 5,
"name": "Michael Dooley",
"email": "hayes.reinhold#example.org",
"phone_number": "0697830800",
"age": "33",
"salary": "265",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 6,
"name": "Prof. Kelley Koepp Jr.",
"email": "kutch.jessie#example.org",
"phone_number": "0697830800",
"age": "34",
"salary": "688",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 7,
"name": "Herminia McClure PhD",
"email": "ywillms#example.com",
"phone_number": "0697830800",
"age": "35",
"salary": "103",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 8,
"name": "Morton Considine",
"email": "lesly.pfeffer#example.com",
"phone_number": "0697830800",
"age": "36",
"salary": "804",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 9,
"name": "Katlyn Muller",
"email": "peter93#example.net",
"phone_number": "0697830800",
"age": "37",
"salary": "695",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 10,
"name": "Wilber Stehr",
"email": "bmckenzie#example.net",
"phone_number": "0697830800",
"age": "38",
"salary": "941",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
},
{
"id": 11,
"name": "Test User",
"email": "test#example.com",
"phone_number": "0697830800",
"age": "60",
"salary": "448",
"created_at": "2023-02-17T08:36:00.000000Z",
"updated_at": "2023-02-17T08:36:00.000000Z"
}
]
function getDevice()
{
return ['data' =>
Client::all(),
};
Please refer to the below solution for every response :
return response()->json('status' => 'success','message' => 'Client listing successfully.','data' => Client::all());
If you know how to use the helper function in Laravel then Please used the below for all responses:
Example :
In Controller, Just add the below line :
return apiResponse('success','Client listed successfully',Client::all()]);
if (!function_exists('apiResponse')) {
function apiResponse($status = null, $message = null, $data = null, $code = 200, $extra = []) {
$response = [
'status' => $status,
'message' => $message,
'data' => $data,
];
if(!empty($extra)){
$response = array_merge($response,$extra);
}
return response()->json($response, $code);
}
}
I think this will help you a lot.

Elasticsearch query for two filters

I am new to Elasticsearch and i have a use case where i need to fetch data for the below 2 conditions
zoneType : [test,oms]
{"geo_bounding_box":{"location":{"top_left":{"lat":"1.3545001078734353","lon":"103.87945999358624"},"bottom_right":{"lat":"1.3435168247600437","lon":"103.89390100692282"}}}
My Query always returns the whole data which is below , but i want my data to be returned only for the above conditions
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 11,
"max_score": 1,
"hits": [
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "chak_01",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lng": 78.460938,
"lat": 25.665325
}
],
"level": 1,
"station_id": "01",
"tag_owner": "xylem",
"tag_network_name": "chak_network",
"supply_zone": "mantena",
"display_name": "Chak 01",
"tag_sector": "sensorstation",
"meta_info": {
"site": {
"site_name": "site Name",
"site_id": "04"
},
"district": {
"district_name": "district Name",
"district_id": "03"
},
"state": {
"state_name": "state Name",
"state_id": "05"
},
"village": {
"village_id": "01",
"village_name": "village Name"
},
"tehsil": {
"tehsil_id": "02",
"tehsil_name": "tehsil Name"
}
},
"tag_category": "sensorstation",
"node_reference": "chak-01",
"_id": "chak_01"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "chak-01",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lng": 78.460938,
"lat": 25.665325
}
],
"level": 1,
"station_id": "01",
"tag_owner": "xylem",
"tag_network_name": "chak_network",
"supply_zone": "mantena",
"display_name": "Chak 01",
"tag_sector": "sensorstation",
"meta_info": {
"site": {
"site_name": "site Name",
"site_id": "04"
},
"district": {
"district_name": "district Name",
"district_id": "03"
},
"state": {
"state_name": "state Name",
"state_id": "05"
},
"village": {
"village_id": "01",
"village_name": "village Name"
},
"tehsil": {
"tehsil_id": "02",
"tehsil_name": "tehsil Name"
}
},
"tag_category": "sensorstation",
"node_reference": "980066547976678",
"_id": "chak-01"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "dummy_elastic_update_station",
"_score": 1,
"_source": {
"dummystnupddate": "Thu Dec 03 2015 07:00:01 GMT+0000",
"level": "1",
"icon": "newicons/dataxicons/blue.png",
"_id": "dummy_elastic_update_station",
"tag_location": "unknown"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "5f3121d6b4c93c1d20bbbb38",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lat": "0",
"lon": "0"
}
],
"level": 1,
"kml_path": "",
"created": "Mon Aug 10 16:00:47 IST 2020",
"latitude": "0",
"station_id": "5f3121d6b4c93c1d20bbbb38",
"longtitude": "0",
"tag_owner": "",
"description": "",
"zoneType": "test",
"tag_network_name": "chak_network",
"display_name": "testname",
"supply_zone": "testname",
"outflow": null,
"tag_sector": "dmameter",
"name": "testname",
"tag_category": "sensorstation",
"inflow": null,
"_id": "5f3121d6b4c93c1d20bbbb38",
"tag_location": "NA",
"lastmod": "Mon Aug 10 16:00:47 IST 2020",
"status": "ACTIVE"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "5f312253b4c93c1d20bbbb39",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lat": "0",
"lon": "0"
}
],
"level": 1,
"kml_path": "",
"created": "Mon Aug 10 16:02:51 IST 2020",
"latitude": "0",
"station_id": "5f312253b4c93c1d20bbbb39",
"longtitude": "0",
"tag_owner": "",
"description": "",
"zoneType": "oms",
"tag_network_name": "chak_network",
"display_name": "506020200236117-O1",
"supply_zone": "506020200236117-O1",
"outflow": null,
"tag_sector": "dmameter",
"name": "506020200236117-O1",
"tag_category": "sensorstation",
"inflow": null,
"_id": "5f312253b4c93c1d20bbbb39",
"tag_location": "NA",
"lastmod": "Mon Aug 10 16:02:51 IST 2020",
"status": "ACTIVE"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "5f32357b3ccb8f51e003587e",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lat": "0",
"lon": "0"
}
],
"level": 1,
"kml_path": "",
"created": "Tue Aug 11 11:36:51 IST 2020",
"latitude": "0",
"station_id": "5f32357b3ccb8f51e003587e",
"longtitude": "0",
"tag_owner": "",
"description": "",
"zoneType": "village",
"display_name": "testvillage1",
"supply_zone": "testvillage1",
"outflow": null,
"tag_sector": "dmameter",
"name": "testvillage1",
"tag_category": "sensorstation",
"inflow": null,
"_id": "5f32357b3ccb8f51e003587e",
"tag_location": "NA",
"lastmod": "Tue Aug 11 11:36:51 IST 2020",
"status": "ACTIVE"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "5ee0861c5f15030001b2dfd9",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lat": "0",
"lon": "0"
}
],
"level": 1,
"kml_path": "",
"created": "Wed Jun 10 07:05:00 UTC 2020",
"latitude": "0",
"station_id": "5ee0861c5f15030001b2dfd9",
"longtitude": "0",
"tag_owner": "",
"description": "",
"tag_network_name": "chak_network",
"display_name": "bhamarhar",
"supply_zone": "bhamarhar",
"outflow": null,
"tag_sector": "dmameter",
"name": "bhamarhar",
"tag_category": "sensorstation",
"inflow": null,
"_id": "5ee0861c5f15030001b2dfd9",
"tag_location": "NA",
"lastmod": "Wed Jun 10 07:05:00 UTC 2020",
"status": "ACTIVE"
}
},
{
"_index": "dataintelindex_man",
"_type": "station_info",
"_id": "5f2ad03bba21eb28684451e3",
"_score": 1,
"_source": {
"tag_datatype": "sensor",
"loc": [
{
"lat": "0",
"lon": "0"
}
],
"level": 1,
"kml_path": "",
"created": "Wed Aug 05 20:58:59 IST 2020",
"latitude": "0",
"station_id": "5f2ad03bba21eb28684451e3",
"longtitude": "0",
"tag_owner": "",
"description": "",
"tag_network_name": "chak_network",
"display_name": "zone-1",
"supply_zone": "zone-1",
"outflow": null,
"tag_sector": "dmameter",
"name": "zone-1",
"tag_category": "sensorstation",
"inflow": null,
"_id": "5f2ad03bba21eb28684451e3",
"tag_location": "NA",
"lastmod": "Wed Aug 05 20:58:59 IST 2020",
"status": "ACTIVE"
}
},
{
"_index": "dataintelindex_man",
"_type": "sensor_info",
"_id": "chak_01_btry",
"_score": 1,
"_source": {
"parent": "chak_01",
"sensortype_units": "volt",
"device_id": "OMS_MP_LRR_001",
"level": 2,
"sensortype_actual": "btry",
"highrate": false,
"tag_datasource": "xylem",
"_id": "chak_01_btry",
"sensortype_display": "btry - chak_01",
"type": "sensor",
"sensortype_backend": "btry"
}
},
{
"_index": "dataintelindex_man",
"_type": "sensor_info",
"_id": "chak-01/pressure",
"_score": 1,
"_source": {
"parent": "chak-01",
"sensortype_units": "bar",
"device_id": "OMS_MP_LRR_001",
"level": 2,
"sensortype_actual": "pressure",
"highrate": false,
"tag_datasource": "xylem",
"_id": "chak-01/pressure",
"sensortype_display": "pressure - chak-01",
"type": "sensor",
"sensortype_backend": "pressure"
}
}
]
}
}
The query which i have formed is as below :
{
"query":{
"geo_bounding_box":{
"location":{
"top_left":{
"lat":"1.3545001078734353",
"lon":"103.87945999358624"
},
"bottom_right":{
"lat":"1.3435168247600437",
"lon":"103.89390100692282"
}
}
}
},
{
"zoneType":[
"oms",
"test"
]
},
"size":100000
}
Please help me validate the query.
There are multiple issues:
JSON is invalid
zoneType is placed at the root of the object, it should be under query
In order for this to work, all queries/filters must be placed within root query object and in order to combine multiple filters, you'll need to use compound queries, in this specific case - bool query, read about it more in the documentation.
So this would be the actual query that should work for you:
{
"query": {
"bool": {
"filter": [
{
"geo_bounding_box": {
"location": {
"top_left": {
"lat": "1.3545001078734353",
"lon": "103.87945999358624"
},
"bottom_right": {
"lat": "1.3435168247600437",
"lon": "103.89390100692282"
}
}
}
},
{
"terms": {
"zoneType": [
"oms",
"test"
]
}
}
]
}
},
"size": 100000
}
See I've moved zoneType under query->bool->filter[], next to geo_bounding_box query.

How to filter laravel collection from given data

i have a collection data
{
"success": true,
"doctor": [
{
"id": 1,
"name": "Dr. Mayank",
"dob": "1975-01-01",
"about": "This is description",
"status": 1,
"rating": 2,
"rating_given_by": 1,
"alternative_number": "7686876876",
"profile_photo": [],
"speciality": [
{
"id": 3,
"name": "Acupuncture",
"image": null,
"dashboard_flag": 1
},
{
"id": 4,
"name": "Acupuncturist",
"image": null,
"dashboard_flag": 1
},
{
"id": 1,
"name": "Accident and emergency medicine",
"image": "http://192.168.16.21/remidify/media/174/detail.png",
"dashboard_flag": 1
}
],
"service": [
{
"id": 78,
"name": "Correction of gummy smile",
"cost": "12.00"
},
{
"id": 77,
"name": "Dental aesthetics",
"cost": "43.00"
}
],
"clinics": [
{
"id": 1,
"name": "akram",
"entity_id": 1,
"entity_type": "App\Doctor",
"contact_number": "2132132132132",
"status": 0,
"consultancy_fee": "12.00",
"available_today": "No",
"owner_name": "Dr. Mayank",
"pivot": {
"doctor_id": 1,
"clinic_id": 1
},
"address": {
"id": 1,
"address_1": "asdasdasdsa",
"address_2": "",
"locality": "downtown",
"city": "noida",
"state": "up",
"postal_code": "41561566"
},
"speciality": [],
"service": [
{
"id": 11,
"name": "Laminates",
"cost": "20.00"
},
{
"id": 12,
"name": "Dental surgery",
"cost": "300.00"
}
],
"clinic_image": [
{
"id": 7,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "clinic_image",
"file_name": "1494863957588.566162.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/7/1494863957588.566162.jpg"
}
],
"id_image": [
{
"id": 8,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "id_image",
"file_name": "1494863966218.348877.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/8/1494863966218.348877.jpg"
}
],
"location": {
"id": 1,
"latitude": 0,
"longitude": 0,
"entity_id": 1,
"entity_type": "App\Clinic",
"created_at": "2017-05-16 03:00:10",
"updated_at": "2017-05-16 03:00:10"
},
"clinic_timings": [
{
"day": "sun",
"opens_at": "09:28:00",
"closes_at": "21:28:00"
}
]
}
],
"education": [
{
"id": 19,
"degree": "MBBS",
"university": "Univercity",
"year": "2017",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:11",
"updated_at": "2017-05-16 05:44:11",
"location": "Delhi"
}
],
"experience": [
{
"id": 19,
"hospital": "Hospital name",
"post": "pta ni hai",
"from": "1970-01-01",
"to": "0000-00-00",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:12",
"updated_at": "2017-05-16 05:44:12",
"location": "Locations12",
"is_currently_working": 1
}
],
"registration": {
"id": 1,
"registration_number": "Reg # 2324324",
"registration_year": 1975,
"registration_council": "Council",
"experience": null,
"doctor_id": 1,
"created_at": "2017-05-16 02:56:37",
"updated_at": "2017-05-16 02:56:37",
"adhaar_number": "232131231232",
"id_proof": [
{
"id": 2,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "id_proof",
"file_name": "1494863680447.329102.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/2/1494863680447.329102.jpg"
}
],
"registration_proof": [
{
"id": 3,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "registration_proof",
"file_name": "1494863687436.266846.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/3/1494863687436.266846.jpg"
}
],
"qualification_proof": [
{
"id": 4,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "qualification_proof",
"file_name": "1494863695576.803955.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/4/1494863695576.803955.jpg"
}
]
},
"preference": {
"availability": 1,
"appointment_confirmation_method": "manual",
"average_time": 7,
"holiday_from": null,
"holiday_till": null,
"patients_per_hour": null,
"preferred_appointment_type": "timeslot",
"appointment_frequency": null,
"preferred_payment_method": [
{
"payment_method": "cash"
},
{
"payment_method": "online"
}
]
},
"user": null,
"doctor_clinic": [
{
"doctor_id": 1,
"clinic_id": 1,
"consultancy_fee": "12.00",
"deleted_at": null,
"workdays": [
{
"day": "sun",
"available": 1,
"workhours": [
{
"from": "09:28:00",
"to": "21:28:00"
}
]
}
],
"service": []
}
]
}
]
}
Now how can i find the doctors whose "about or specialty name" matches with some given search string.
Thanks in advance.
Try using dd() helper and use like this below
$youcollectionvariables = { "success": true, "doctor": [ { "id": 1, "name": "Dr. Mayank", "dob": "1975-01-01"................. }
dd($youcollectionvariables)
you will see a formatted data and can find what you need clearly.
Hope that helps.

ElasticSearch apply should and range

My situation:
I'm working with an ElasticSearch database and I cant apply a couple of "ORs" plus a couple of "ANDs". I'm writing the SQL query to show what I want, in my SQL query I've used confirmedPlayers and pendingPlayers as they were arrays, of course I know we cant do that in SQL, but I just wanted to take an example.
If you want me to add my mappings, I will, It is just I dont want to make extensive the post.
This is my query in SQL:
SELECT *
FROM match
WHERE (
"AVnJOMvXOX1s7Ny2Wu9O" in confirmedPlayers OR
"AVnJOMvXOX1s7Ny2Wu9O" in pendingPlayers OR
"AVnJOMvXOX1s7Ny2Wu9O" = creator
)
AND date >= "20/01/2016"
/* AND other filter will be added */
This is my match type info:
{
"took": 79,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 1,
"hits": [
{
"_index": "yojuego",
"_type": "match",
"_id": "AVmak0bWIjogo0aNpbGs",
"_score": 1,
"_source": {
"title": "Mi primer match",
"date": "2016-01-13T20:31:20.000Z",
"fromTime": "19:00",
"toTime": "20:00",
"location": "casa de pablo",
"creator": "AVmabq-5Ijogo0aNpbGn",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVm0ETbT0Y26YggShbFa",
"_score": 1,
"_source": {
"title": "Mi primer match",
"date": "2016-01-13T20:31:20.000Z",
"fromTime": "19:00",
"toTime": "20:00",
"location": "casa de pablo",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [
"AVmBKi21XRKVuACJGZZZ",
"AVmabq-5Ijogo0aNpbGn"
],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVmab1G5Ijogo0aNpbGo",
"_score": 1,
"_source": {
"title": "Mi primer match",
"date": "2016-01-13T20:31:20.000Z",
"fromTime": "19:00",
"toTime": "20:00",
"location": "casa de pablo",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [
"AVmabVjUIjogo0aNpbGm",
"AVmBKi21XRKVuACJGZZZ"
],
"pendingPlayers": [
"AVmBKi21XRKVuACJGZZZ"
],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVm0EPX20Y26YggShbFZ",
"_score": 1,
"_source": {
"title": "Mi primer match",
"date": "2016-01-13T20:31:20.000Z",
"fromTime": "19:00",
"toTime": "20:00",
"location": "casa de pablo",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [
"AVmabVjUIjogo0aNpbGm",
"AVmabq-5Ijogo0aNpbGn"
],
"pendingPlayers": [
"AVmBKi21XRKVuACJGZZZ"
],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "match",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVnJOMvXOX1s7Ny2Wu9O",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-9fOJxj9yxI50RS3",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [
"AVnJOMvXOX1s7Ny2Wu9O"
],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-ykMJxj9yxI50RS1",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVnJOMvXOX1s7Ny2Wu9O",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-73OJxj9yxI50RS2",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [
"AVnJOMvXOX1s7Ny2Wu9O"
],
"pendingPlayers": [],
"comments": []
}
}
]
}
}
This query returns 4 matches, and it is OK.
http://localhost:9200/my_index/match
POST _search
{
"query": {
"bool": {
"should": [
{ "term": { "confirmedPlayers": { "value": "AVnJOMvXOX1s7Ny2Wu9O" } } },
{ "term": { "pendingPlayers": { "value": "AVnJOMvXOX1s7Ny2Wu9O" } } },
{ "term": { "creator": { "value": "AVnJOMvXOX1s7Ny2Wu9O" } } }
],
"must": [
{ "range": { "date": { "gte": "20/01/2016", "format": "dd/MM/yyyy" } } }
]
}
}
}
//RESULT
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.6931472,
"hits": [
{
"_index": "yojuego",
"_type": "match",
"_id": "match",
"_score": 1.6931472,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVnJOMvXOX1s7Ny2Wu9O",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-73OJxj9yxI50RS2",
"_score": 1.6931472,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [
"AVnJOMvXOX1s7Ny2Wu9O"
],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-9fOJxj9yxI50RS3",
"_score": 1.287682,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [
"AVnJOMvXOX1s7Ny2Wu9O"
],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-ykMJxj9yxI50RS1",
"_score": 1.287682,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVnJOMvXOX1s7Ny2Wu9O",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
}
]
}
}
But this query is returning 4 matches too, and this is the case where it should not return anything.
POST _search
{
"query": {
"bool": {
"should": [
{ "term": { "confirmedPlayers": { "value": "inexistant" } } },
{ "term": { "pendingPlayers": { "value": "inexistant" } } },
{ "term": { "creator": { "value": "inexistant" } } }
],
"must": [
{ "range": { "date": { "gte": "20/01/2016", "format": "dd/MM/yyyy" } } }
]
}
}
}
//RESULT
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "yojuego",
"_type": "match",
"_id": "match",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVnJOMvXOX1s7Ny2Wu9O",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-9fOJxj9yxI50RS3",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [
"AVnJOMvXOX1s7Ny2Wu9O"
],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-ykMJxj9yxI50RS1",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVnJOMvXOX1s7Ny2Wu9O",
"matchType": "5",
"confirmedPlayers": [],
"pendingPlayers": [],
"comments": []
}
},
{
"_index": "yojuego",
"_type": "match",
"_id": "AVnm-73OJxj9yxI50RS2",
"_score": 1,
"_source": {
"title": "otro match 3",
"date": "2017-12-28T00:00:00.000Z",
"fromTime": "21:00",
"toTime": "22:00",
"location": "somewhere",
"creator": "AVmabVjUIjogo0aNpbGm",
"matchType": "5",
"confirmedPlayers": [
"AVnJOMvXOX1s7Ny2Wu9O"
],
"pendingPlayers": [],
"comments": []
}
}
]
}
}
Mappings:
{
"match": {
"properties": {
"title": { "type": "string" },
"date": { "type": "date" },
"fromTime": { "type": "string" },
"toTime": { "type": "string" },
"location": { "type": "string" },
"matchType": { "type": "integer" },
"creator": {
"type": "string",
"index": "not_analyzed"
},
"confirmedPlayers" : {
"type": "string",
"index": "not_analyzed"
},
"pendingPlayers" : {
"type": "string",
"index": "not_analyzed"
},
"comments" : {
"properties" : {
"id" : { "type" : "integer" },
"owner" : { "type" : "string" },
"text" : { "type" : "string" },
"writtenOn": { "type": "date" }
}
}
}
}
}
The problem cames up when I use should and must all togheter. If I use should and must separately they work fine.
Based on the result of your second example query (where you claim that 0 results should be returned), it seems you have some confusion about the way that should works in elasticsearch.
I'll quote from the documentation
should
The clause (query) should appear in the matching document. In a
boolean query with no must or filter clauses, one or more should
clauses must match a document. The minimum number of should clauses to
match can be set using the minimum_should_match parameter.
If you use a query with a should and a must, it isn't actually necessary that the should clause hits, only the must clause. If the should clauses do happen to hit, they will be ranked higher in the results.
You have options though. One option: you can write a simple should query, and set the minimum_should_match parameter, then wrap that query in a filtered clause to filter based on the date. Second option: create a nested query, with the must clause inside the should clause.

Aggregation in elasticsearch with specific parameter

I have bulk documents in elasticsearch and as an example I have taken the elasticsearch documentation example as banks
{
"_index": "bank",
"_type": "account",
"_id": "25",
"_score": 1,
"_source": {
"account_number": 25,
"balance": 40540,
"firstname": "Virginia",
"lastname": "Ayala",
"age": 39,
"gender": "F",
"address": "171 Putnam Avenue",
"employer": "Filodyne",
"email": "virginiaayala#filodyne.com",
"city": "Nicholson",
"state": "PA"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "44",
"_score": 1,
"_source": {
"account_number": 44,
"balance": 34487,
"firstname": "Aurelia",
"lastname": "Harding",
"age": 37,
"gender": "M",
"address": "502 Baycliff Terrace",
"employer": "Orbalix",
"email": "aureliaharding#orbalix.com",
"city": "Yardville",
"state": "DE"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "99",
"_score": 1,
"_source": {
"account_number": 99,
"balance": 47159,
"firstname": "Ratliff",
"lastname": "Heath",
"age": 39,
"gender": "F",
"address": "806 Rockwell Place",
"employer": "Zappix",
"email": "ratliffheath#zappix.com",
"city": "Shaft",
"state": "ND"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "119",
"_score": 1,
"_source": {
"account_number": 119,
"balance": 49222,
"firstname": "Laverne",
"lastname": "Johnson",
"age": 28,
"gender": "F",
"address": "302 Howard Place",
"employer": "Senmei",
"email": "lavernejohnson#senmei.com",
"city": "Herlong",
"state": "DC"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "126",
"_score": 1,
"_source": {
"account_number": 126,
"balance": 3607,
"firstname": "Effie",
"lastname": "Gates",
"age": 39,
"gender": "F",
"address": "620 National Drive",
"employer": "Digitalus",
"email": "effiegates#digitalus.com",
"city": "Blodgett",
"state": "MD"
}
}
Now there is a field called state and price in each document. How can I write a query for which it returns only the results that contain distinct state with sort order as balance in asc order.
I was trying with terms aggregation but of no use.
UPDATE
POST _search
{
"size": 0,
"aggs": {
"states": {
"terms": {
"field": "state"
},
"aggs": {
"balances": {
"top_hits": {
"from" : 0,
"size": 1,
"sort": {"balance": "asc"}
}
}
}
}
}
}
now for this query i'll be returned with all top-hits with price sorted in that key "state". But what i want is a sorted results w.r.t balance and with unique state fields.
For the above query, i am getting response as follows
"buckets": [
{
"key": "tx",
"doc_count": 30,
"balances": {
"hits": {
"total": 30,
"max_score": null,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "161",
"_score": null,
"_source": {
"account_number": 161,
"balance": 4659,
"firstname": "Doreen",
"lastname": "Randall",
"age": 37,
"gender": "F",
"address": "178 Court Street",
"employer": "Calcula",
"email": "doreenrandall#calcula.com",
"city": "Belmont",
"state": "TX"
},
"sort": [
4659
]
}
]
}
}
},
{
"key": "md",
"doc_count": 28,
"balances": {
"hits": {
"total": 28,
"max_score": null,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "527",
"_score": null,
"_source": {
"account_number": 527,
"balance": 2028,
"firstname": "Carver",
"lastname": "Peters",
"age": 35,
"gender": "M",
"address": "816 Victor Road",
"employer": "Housedown",
"email": "carverpeters#housedown.com",
"city": "Nadine",
"state": "MD"
},
"sort": [
2028
]
}
]
}
}
},
{
"key": "id",
"doc_count": 27,
"balances": {
"hits": {
"total": 27,
"max_score": null,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "402",
"_score": null,
"_source": {
"account_number": 402,
"balance": 1282,
"firstname": "Pacheco",
"lastname": "Rosales",
"age": 32,
"gender": "M",
"address": "538 Pershing Loop",
"employer": "Circum",
"email": "pachecorosales#circum.com",
"city": "Elbert",
"state": "ID"
},
"sort": [
1282
]
}
]
}
}
},
which is not in price sorted.
Try like this:
POST bank/_search
{
"size": 0,
"aggs": {
"states": {
"terms": {
"field": "state",
"order": {
"balances": "asc"
}
},
"aggs": {
"balances": {
"sum": {
"field": "balance"
}
}
}
}
}
}
Note: I don't see a price field, but a balance one, maybe that's the one you meant.
If you're interested in getting all documents by state sorted by price, then you can try this, too:
POST bank/_search
{
"size": 0,
"aggs": {
"states": {
"terms": {
"field": "state"
},
"aggs": {
"balances": {
"top_hits": {
"size": 5,
"sort": {"balance": "asc"}
}
}
}
}
}
}

Resources