Friday, June 24, 2016

Some experience while working with ElasticSearch, Angular

In this week, i join one project need program web portal. My hobby is Python so i chose Flask, Angular JS. I`m using bootstrap to make css template.
My DB is ElasticSearch. I don`t use http request to make query ES, i used python-es lib.
So, when use ES, i have some problems.
1. Sort in ES:
            data = self.es.search(index=self.indexName, doc_type=self.docType,
                                  body={"query": {
                                      "filtered": {
                                          "query": {
                                              "bool": {
                                                  "must": mustDict,
                                              },
                                          }
                                      }
                                  },
                                      "size": size, "from": from_, "sort": {"timestamp":{"order":"desc"}}
                                  },
                                  )
2. AND/OR operator: use bool.
AND like must. I created must list.
3. To make Restful API, i used flask_restful and integrated with my flask. Coding like web MVC. Blueprint to route and create controller, then render HTML. In client, call restful api to get data. Very simple.
4. Angular JS re-render:
I used start-angular theme, and it have table responsive. All data in
tag. And it will be filled by Angular JS. But table responsive must render after Angular render. So i found one trick use directive and setTimeout:

    function reFormatTable() {
        $('#dataTables-example').DataTable({
            responsive: true
        });
    }
    var app = angular.module('myApp', [])
            .directive('myRepeatDirective', function () {
                return function (scope, element, attrs) {
                };
            }).directive('myMainDirective', function () {
                return function (scope, element, attrs) {
                    setTimeout(reFormatTable, 1000);
                };
            });
5. Call angular function in template:
I like program in angular template, so i need angular function. To map:
        $scope.isObject = angular.isObject;
        $scope.isString = angular.isString;
        $scope.isDefined = angular.isDefined;
In HTML: ng-if = "isObject(object)"
6. Find string in field:
When searching, i need find string in one field. I tried with regex, * character but not success. Finally, i found wildcard:
mustDict.append({"wildcard" : { "site" : { "value" : "*{0}*".format(sitename), "boost" : 2.0 }}})
Search in site field, with value have string sitename. I added to mustDict.
----------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
SecurityLab - Linux Lab -- Window and Cisco Lab
to be continued - I will update more.

No comments:

Install Xposed Inspector and Frida on Genymotion

Today i had some work with android. So i need trace application. I found 2 nice tool can help me: Xposed Inspector and Frida. To setup ther...