Skip to content
Advertisement

Extend JSON object by multiple JSON levels

trying to extend existing empty JSON object.

After first 2 simple extends I will get {size: 10, from: 0}.

But when trying $.extend function with multiple level JSON

"query" : 
  { "query_string" : 
    { "fields" : ["name", "description"], 
      "query" : "d.search.value", 
      "tie_breaker" : 0 
    }
  }

I have no success.

            data : function ( d ) {
                var query = '{}';

                sQuery = $.extend(query,{size: d.length});
                sQuery = $.extend(sQuery,{from: d.start});
                
                if ( d.search.value ) {     
                
                    sQuery = $.extend(sQuery,                   
                        "query" : { "query_string" : { "fields" : ["name", "description"], "query" : "d.search.value", "tie_breaker" : 0 } }
                    );
                    console.log(sQuery);
                };
                    
            return JSON.stringify(sQuery);
            
        },                      

But getting “Uncaught SyntaxError: missing ) after argument list” error.

I believe overlooked something simple but I am not able to find it.

Advertisement

Answer

Thanks to @Barmar I have final working solution:

$('#integrations').DataTable( {
    var query = '{}';

    "bProcessing": true,
    "bServerSide": true,
    "ajax": {
        "url": "http://localhost:9200/integrations-prod/_search",

        data : function ( d ) {
            console.log( d.length );
            sQuery = $.extend(query,{size: d.length});
            sQuery = $.extend(sQuery,{from: d.start});
            
            if ( d.search.value ) {     
                sQuery = $.extend(sQuery,                   
                {"query" : { "query_string" : { "fields" : ["name", "description"], "query" : d.search.value, "tie_breaker" : 0 } }}
                );
            };
            return JSON.stringify(sQuery);
        },
        "contentType": "application/json",
        "type": "POST",
        "dataType": "json",
        "dataSrc": "hits.hits",
        dataFilter: function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.hits.total.value;
            json.recordsFiltered = json.hits.total.value;
            return JSON.stringify( json ); // return JSON string
        },
        "columnDefs": [ { "defaultContent": "-", "targets": "_all" } ],
    },
    "columns": [
        { "data": "_source.name" },
        { "data": "_source.description" },
        { "data": "_source.valid" },
        { "data": "_source.status_id" },
        { "data": "_source.region_id" },
        { "data": "_source.validated_time" }
    ]
} );

This “accidently” resolve partially also cases Elasticsearch Datatables, Datatables pagination with Elasticsearch, dataTable custom loading contents, …

Thank you @Barmar

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement