Tutorial: Creating a Search Query

Creating a Search Query



This tutorial explains how to use sap.bc.ina.api.impl.inav2.SearchQuery class and its result.

First declare your sina variable and define the right dataSource object. To define SAP HANA analytical or attribute views as data source, you need the package name and the object name.

Declaration and definition of sina and data source

var sina = sap.bc.ina.api.sina;
var dataSource = {
    "packageName" : "sap.bc.ina.demos.epm.views",
    "objectName"  : "V_EPM_PRODUCT"
};

In the next step, create the query. In this example, you call the method without any properties and add the properties using setters. The return value this allows method chaining.

Creating the query

var query = sina.createSearchQuery()
.dataSource(dataSource)
.top(5);

Once the query is created, you can add search properties. You add the attributes that you want to get back in the response, and some filter conditions that restrict the results. At minimum, you have to add your search term.

Adding attributes, filters and search terms

query.addResponseAttribute("CATEGORY")
.addResponseAttribute("TEXT")
.addResponseAttribute("PRICE")
.addFilterCondition("CATEGORY", "=", "Notebooks")
.addFilterCondition("PRICE","<","1000")
.addFilterCondition("CURRENCY_CODE", "=", "EUR")
.setSearchTerms("basic");

When the query was sent, you get back a result set. The first parameter of the method is the callback function on success. If everything went right, you get one object with your all your results. If not, the second parameter is the callback function on error.

query.getResultSet(function (resSet){
    // --- do something
    console.log(resSet);
}, function (jqxhr){
    // --- do something
    console.log(jqxhr);
});

The result object looks like this:

resSet = {
    elements : [
        0 : {
            "CATEGORY" : {
                label : "CATEGORY",
                valueRaw : "Notebooks",
                value : "Notebooks"
            },
            "TEXT" : {
                label : "TEXT",
                valueRaw : "Notebook Basic 15 with 1 /7GHz - 15",
                value : "Notebook Basic 15 with 1 /7GHz - 15"
            },
            "PRICE" : {
                label : "PRICE"
                valueRaw : 956
                value : "956"
            }
        },
        1 : {
            "CATEGORY" : {
                label : "CATEGORY",
                valueRaw : "Notebooks",
                value : "Notebooks"
            },
            "TEXT" : {
                label : "TEXT",
                valueRaw : "1 /5 Ghz / single core / 40 GB HDD / Windows Vista Home Basic / 512 MB RAM",
                value : "1 /5 Ghz / single core / 40 GB HDD / Windows Vista Home Basic / 512 MB RAM"
            },
            "PRICE" : {
                label : "PRICE",
                valueRaw : 799,
                value : "799"
            }
        }
    ],
    totalcount : 2
}

Complete example

<html>
    <head>
        <meta charset="utf-8">
        <title>Simple Information Access</title>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
        <script src="/sap/bc/ina/api/sina/release/sap.bc.ina.api.sina.min.js" type="text/javascript"></script>
        <script>
            var sina = sap.bc.ina.api.sina;
            var dataSource = {
                "packageName" : "sap.bc.ina.demos.epm.views",
                "objectName"  : "V_EPM_PRODUCT"
            };
            var query = sina.createSearchQuery()
            .dataSource(dataSource)
            .top(5);

            query.addResponseAttribute("CATEGORY")
            .addResponseAttribute("TEXT")
            .addResponseAttribute("PRICE")
            .addFilterCondition("CATEGORY", "=", "Notebooks")
            .addFilterCondition("PRICE","<","1000")
            .addFilterCondition("CURRENCY_CODE", "=", "EUR")
            .setSearchTerms("basic");

            query.getResultSet(function (rs){
                console.log(rs);
            }, function (jqxhr){
                console.log(jqxhr);
            });
        </script>
    </head>
    <body>
        <!-- do something -->
    </body>
</html>