jqgrid treeGrid Cannot read property 'rowIndex' of undefined - jqgrid

I try to use local data with treeGrid, when i click the unfold
button . I get the tips "Cannot read property 'rowIndex' of
undefined"

<!DOCTYPE html>
<html lang="en">
<head>
<!-- The jQuery library is a prerequisite for all jqSuite products -->
<script type="text/ecmascript" src="./js/jquery.min.js"></script>
<!-- This is the Javascript file of jqGrid -->
<script type="text/ecmascript" src="./js/jquery.jqGrid.min.js"></script>
<!-- This is the localization file of the grid controlling messages, labels, etc.
<!-- We support more than 40 localizations -->
<script type="text/ecmascript" src="./js/i18n/grid.locale-en.js"></script>
<!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom -->
<link rel="stylesheet" type="text/css" media="screen" href="./css/jquery-ui-1.10.4.custom.min.css" />
<!-- The link to the CSS that the grid needs -->
<link rel="stylesheet" type="text/css" media="screen" href="./css/ui.jqgrid.css" />
<meta charset="utf-8" />
<title>jqTreeGrid - Load On Demand - Load all Rows at once collapsed</title>
</head>
<body>
<table id="tree"></table>
<div id="pager"></div>
<script type="text/javascript">
var rows = [
{
"category_id": "1",
"name": "ELECTRONICS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "1",
"rgt": "44",
"level": "0",
"uiicon": ""
},
{
"category_id": "2",
"name": "TELEVISIONS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "2",
"rgt": "19",
"level": "1",
"uiicon": ""
},
{
"category_id": "3",
"name": "TUBE",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "3",
"rgt": "8",
"level": "2",
"uiicon": ""
},
{
"category_id": "11",
"name": "26 \" TV",
"price": "200.00",
"qty_onhand": "1",
"color": "black",
"lft": "4",
"rgt": "5",
"level": "3",
"uiicon": "ui-icon-image"
},
{
"category_id": "12",
"name": "30 \" TV",
"price": "350.00",
"qty_onhand": "2",
"color": "black",
"lft": "6",
"rgt": "7",
"level": "3",
"uiicon": "ui-icon-document"
},
{
"category_id": "4",
"name": "LCD",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "9",
"rgt": "12",
"level": "2",
"uiicon": ""
},
{
"category_id": "13",
"name": "Super-LCD 42\" ",
"price": "400.00",
"qty_onhand": "10",
"color": "all",
"lft": "10",
"rgt": "11",
"level": "3",
"uiicon": "ui-icon-video"
},
{
"category_id": "5",
"name": "PLASMA",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "13",
"rgt": "18",
"level": "2",
"uiicon": ""
},
{
"category_id": "14",
"name": "Ultra-Plasma 62\" ",
"price": "440.00",
"qty_onhand": "2",
"color": "silver",
"lft": "14",
"rgt": "15",
"level": "3",
"uiicon": "ui-icon-clipboard"
},
{
"category_id": "15",
"name": "Value Plasma 38\" ",
"price": "312.00",
"qty_onhand": "0",
"color": "silver",
"lft": "16",
"rgt": "17",
"level": "3",
"uiicon": "ui-icon-clipboard"
},
{
"category_id": "6",
"name": "PORTABLE ELECTRONICS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "20",
"rgt": "43",
"level": "1",
"uiicon": ""
},
{
"category_id": "7",
"name": "MP3 PLAYERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "21",
"rgt": "32",
"level": "2",
"uiicon": ""
},
{
"category_id": "8",
"name": "FLASH",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "22",
"rgt": "29",
"level": "3",
"uiicon": ""
},
{
"category_id": "17",
"name": "Super-Shuffle 1gb",
"price": "20.00",
"qty_onhand": "11",
"color": "all",
"lft": "23",
"rgt": "24",
"level": "4",
"uiicon": "ui-icon-note"
},
{
"category_id": "21",
"name": "5Gb Flash",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "25",
"rgt": "26",
"level": "4",
"uiicon": "ui-icon-comment"
},
{
"category_id": "22",
"name": "10Gb flash ",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "27",
"rgt": "28",
"level": "4",
"uiicon": "ui-icon-tag"
},
{
"category_id": "16",
"name": " Power-MP3 128mb",
"price": "123.00",
"qty_onhand": "2",
"color": "withe",
"lft": "30",
"rgt": "31",
"level": "3",
"uiicon": "ui-icon-signal-diag"
},
{
"category_id": "9",
"name": "CD PLAYERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "33",
"rgt": "38",
"level": "2",
"uiicon": ""
},
{
"category_id": "18",
"name": " Porta CD ",
"price": "10.00",
"qty_onhand": "0",
"color": "",
"lft": "34",
"rgt": "35",
"level": "3",
"uiicon": "ui-icon-eject"
},
{
"category_id": "19",
"name": "CD To go!",
"price": "110.00",
"qty_onhand": "11",
"color": "",
"lft": "36",
"rgt": "37",
"level": "3",
"uiicon": "ui-icon-power"
},
{
"category_id": "10",
"name": "2 WAY RADIOS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "39",
"rgt": "42",
"level": "2",
"uiicon": ""
},
{
"category_id": "20",
"name": "Family Talk 360 ",
"price": "200.00",
"qty_onhand": "15",
"color": "",
"lft": "40",
"rgt": "41",
"level": "3",
"uiicon": "ui-icon-volume-on"
},
{
"category_id": "23",
"name": "COMPUTERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "45",
"rgt": "50",
"level": "0",
"uiicon": ""
},
{
"category_id": "25",
"name": "DESKTOP ",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "46",
"rgt": "47",
"level": "1",
"uiicon": ""
},
{
"category_id": "26",
"name": "LAPTOPS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "48",
"rgt": "49",
"level": "1",
"uiicon": ""
},
{
"category_id": "24",
"name": "APPLIANCES",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "51",
"rgt": "52",
"level": "0",
"uiicon": ""
}
]
jQuery(document).ready(function ($) {
jQuery('#tree').jqGrid({
// "url":"data.json",
"colModel": [
{
"name": "category_id",
"index": "accounts.account_id",
"sorttype": "int",
"key": true,
"hidden": true,
"width": 50
}, {
"name": "name",
"index": "name",
"sorttype": "string",
"label": "Name",
"width": 170
}, {
"name": "price",
"index": "price",
"sorttype": "numeric",
"label": "Price",
"width": 90,
"align": "right"
}, {
"name": "qty_onhand",
"index": "qty_onhand",
"sorttype": "int",
"label": "Qty",
"width": 90,
"align": "right"
}, {
"name": "color",
"index": "color",
"sorttype": "string",
"label": "Color",
"width": 100
}, {
"name": "lft",
"hidden": true
}, {
"name": "rgt",
"hidden": true
}, {
"name": "level",
"hidden": true
}, {
"name": "uiicon",
"hidden": true
}
],
"width": "780",
"hoverrows": false,
"viewrecords": false,
"gridview": true,
"height": "auto",
"sortname": "lft",
"loadonce": true,
"rowNum": 100,
"scrollrows": true,
// enable tree grid
"treeGrid": true,
// which column is expandable
"ExpandColumn": "name",
// datatype
"treedatatype": "json",
// the model used
"treeGridModel": "nested",
// configuration of the data comming from server
"treeReader": {
"left_field": "lft",
"right_field": "rgt",
"level_field": "level",
"leaf_field": "isLeaf",
"expanded_field": "expanded",
"loaded": "loaded",
"icon_field": "icon"
},
// "sortorder": "asc",
"datatype": "local",
"data": rows,
"pager": "#pager"
});
});
</script>
</body>
</html>

For localdata type, you must configure the localReader parameter:
localReader: {
repeatitems: true,
// cell: "",
id: "category_id"
}

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.

How to get one element from array of objects in 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"
}
}
]
}
}
}
}
]
}
}

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.

Elasticsearch document sorting based on nested values

I have the following data indexed in my elasticsearch version 7.4.
[
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Chennai"
},
{
"age": "25",
"dept": "Accounts",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Finance",
"city": "Kochi"
}
],
"isActive": true
},
{
"users": [
{
"age": "23",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "28",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Kochi"
},
{
"age": "25",
"dept": "Finance",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Accounts",
"city": "Chennai"
}
],
"isActive": true
},
{
"users": [
{
"age": "21",
"dept": "IT",
"city": "Kochi"
},
{
"age": "45",
"dept": "Accounts",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Finance",
"city": "Chennai"
}
],
"isActive": false
}
]
the users is nested type
I want to sort the documents based on the following condition
isActive == true && users.dept == "IT" && users.city == "Chennai" with the age sorts in ascending
I am expecting the following result
[
{
"users": [
{
"age": "23",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "28",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Chennai"
},
{
"age": "25",
"dept": "Accounts",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Finance",
"city": "Kochi"
}
],
"isActive": true
},
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Kochi"
},
{
"age": "25",
"dept": "Finance",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Accounts",
"city": "Chennai"
}
],
"isActive": true
}
]
I tried with script based sorting, I couldn't able to achieve the result.
Please help me to sort this out.
Thanks in advance.

How do I extract unique data from Hyperledger Composer using query or js

I want to get unique data from a duplicates data at Hyperledger Composer. How can I achieve this?
suppose I have a data like this
[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "2",
"dataType": "in",
"productName": “A",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "4",
"dataType": "in",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
]
what I want to get is a list of unique Product name.
So the result I want maybe something like this
[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
]
I don't really care about the sequence. I just want to know what productName do I have in my database.
I think this needs something to do with the logic.js part. I can do queries and get the data using post method. But I don't know how to do that using logic.js
I don't know how to get the data from logic.js. The example at official page only delete or trade. They don't return a new data.
any help is appreciated. I want to know how to send data using the js.
The js code to get the unique productname from your data would be
const data =[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
];
const distinctProductName = [...new Set(data.map(x => x.productName))];
You will have your distinct ProductName in distinctProductName but you won't have the details of product. From your questions, it didnot feel necessary too. If you need the details too, you could do something like
const data =[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
];
const result = Array.from(new Set(data.map(x => x.productName)))
.map(productName => {
return{
productName: productName,
dataType : data.find(x => x.productName ===productName).dataType
//you can obtain other values as I obtained dataType
};
});
You will have your data in result.

Resources