# ~~EdgesIntegration:Library~~
# ~~-> Edges:Technology~~
# ~~-> Elasticsearch:Technology~~

from urllib import parse
import json


def make_url_query(**params):
    query = make_query(**params)
    return parse.quote_plus(json.dumps(query))


def make_query(**params):
    gsq = GeneralSearchQuery(**params)
    return gsq.query()


class GeneralSearchQuery(object):
    def __init__(self, terms=None, query_string=None):
        self.terms = None if terms is None else terms if isinstance(terms, list) else [terms]
        self.query_string = query_string

    def query(self):
        musts = []
        if self.terms is not None:
            for term in self.terms:
                musts.append({"terms" : term})

        query = {"match_all" : {}}
        if self.query_string is not None:
            query = {"query_string" : {"default_operator" : "AND", "query" : self.query_string}}

        if len(musts) > 0:
            query = {
                "bool" : {
                    "filter" : {
                        "bool": {
                            "must": musts
                        }
                    },
                    "must" : query
                }
            }

        return { "query" : query }
@@ -714,43 +714,8 @@
 %7D)%0A%0A
-        query = %7B%22match_all%22 : %7B%7D%7D%0A
     
@@ -761,28 +761,25 @@
            q
-uery
+s
  = %7B%22query_s
@@ -846,25 +846,24 @@
 tring%7D%7D%0A
-%0A
         
 if len(m
@@ -858,75 +858,63 @@
     
-if len(musts) %3E 0:%0A            query = %7B%0A                %22boo
+    musts.append(qs)%0A%0A        query = %7B%22match_al
 l%22
- 
 : %7B
+%7D%7D
 %0A   
@@ -910,32 +910,51 @@
 l%22: %7B%7D%7D%0A        
+if len(musts) %3E 0:%0A
             %22fil
@@ -953,18 +953,15 @@
     
-%22filter%22 :
+query =
  %7B%0A 
@@ -979,22 +979,15 @@
     
-        
 %22bool%22
+ 
 : %7B%0A
@@ -1002,32 +1002,24 @@
             
-     
-   
 %22must%22: must
@@ -1024,92 +1024,8 @@
 sts%0A
-                        %7D%0A                    %7D,%0A                    %22must%22 : query%0A
     

fix bug with url query generator for edge interfaces