# Path: dopplr/solr/querybuilder.py
# class QueryBuilder(object):
#     """
#     The Solr query builder.
#     """
# 
#     def __init__(self, *args, **kwargs):
#         """
#         Initialize me.
# 
#         The `*args` parameter allows you to write queries like this::
# 
#             QueryBuilder(Query('*:*'), FilterQuery('field:TEST'),
#                 callback=callback)
# 
#         The `response_mapper` will be called with the complete `Solr` response
#         so you may map it into some internal format. If not given, the solr
#         response will be passed to the `callback`.
#         """
#         self.__has_q = False
# 
#         self.__queries = []
#         for q in args:
#             self.add(q)
# 
#         self.__params = []
#         if len(kwargs.get('fields', [])) > 0:
#             self.__params.append(('fl', ','.join(kwargs.get('fields'))))
#         if kwargs.get('sort', None):
#             self.__params.append(('sort', kwargs.get('sort')))
# 
#         self.__rows = kwargs.get('rows', 10)
# 
#         self.response_mapper = kwargs.get('response_mapper', lambda x: x)
#         self.headers = kwargs.get('headers', None)
# 
#     def add_param(self, param):
#         """
#         Directly add a parameter to the `QueryBuilder`. This is intended for
#         stuff like `dismax` parametrization etc.
# 
#         `param` needs to be a tuple of the form::
#             (name, value)
#         """
#         self.__params.append(param)
# 
#     def add_params(self, params):
#         """
#         Directly add a parameter list to the `QueryBuilder`. This is intended
#         for stuff like `dismax` parametrization etc.
# 
#         `params` needs to be a list of tuples of the form::
#             (name, value)
#         """
#         self.__params.extend(params)
# 
#     def add(self, query):
#         """
#         Add a query objet to the `QueryBuilder`.
#         """
#         if not isinstance(query, BaseQuery):
#             raise TypeError
#         if isinstance(query, Query):
#             if self.__has_q:
#                 raise TooManyQs
#             self.__has_q = True
#         self.__queries.append(query)
# 
#     def get_params(self):
#         """
#         Compute and return a list of all query values.
#         """
#         queries = []
#         queries.extend(self.__params)
#         if not self.__has_q:
#             queries.append(('q', '*:*'))
#         has_facet_on = False
#         has_rows = False
#         for q in self.__queries:
#             for name, value in q.get_params():
#                 if name == 'facet':
#                     if not has_facet_on:
#                         has_facet_on = True
#                     else:
#                         continue
#                 if name == 'rows':
#                     has_rows = True
#                 queries.append((name, value))
# 
#         if not has_rows:
#             queries.append(('rows', str(self.__rows)))
#         return queries


import logging
import json
import urllib
import types
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.httputil import HTTPHeaders
from tornado.ioloop import IOLoop
from .querybuilder import QueryBuilder
        else:
            self._get(final_url, headers=querybuilder.headers,
                callback=handle_search_response(querybuilder, callback))

    def term_suggest(self, querybuilder, callback=None):
        """
        simple query against the /term_suggest requesthandler from solr.
        """
        query_params = self._get_params(querybuilder)

        log.debug('term_suggest with params: %s' % query_params)
        qs = urllib.urlencode(query_params)
        final_url = '?'.join([self._termsuggest_url, qs])
        log.debug('Final suggest URL: %s' % final_url)

        self._get(final_url, headers=querybuilder.headers,
            callback=handle_suggest_response(querybuilder, callback))

    def get_ids(self, ids, fields=None, response_mapper=None, callback=None):
        """
        Using the Solr 4.X realtime /get handler:
        https://wiki.apache.org/solr/RealTimeGet
        """
        log.debug('realtime get with ids: %s' % ids)
        params = [('ids', ','.join(map(unicode, ids)))]
        if fields:
            params.append(('fl', ','.join(fields)))
        qs = urllib.urlencode(params)
        final_url = '?'.join([self._get_url, qs])
        log.debug('Final get URL: %s' % final_url)

# Path: dopplr/solr/querybuilder.py
# class QueryBuilder(object):
#     """
#     The Solr query builder.
#     """
# 
#     def __init__(self, *args, **kwargs):
#         """
#         Initialize me.
# 
#         The `*args` parameter allows you to write queries like this::
# 
#             QueryBuilder(Query('*:*'), FilterQuery('field:TEST'),
#                 callback=callback)
# 
#         The `response_mapper` will be called with the complete `Solr` response
#         so you may map it into some internal format. If not given, the solr
#         response will be passed to the `callback`.
#         """
#         self.__has_q = False
# 
#         self.__queries = []
#         for q in args:
#             self.add(q)
# 
#         self.__params = []
#         if len(kwargs.get('fields', [])) > 0:
#             self.__params.append(('fl', ','.join(kwargs.get('fields'))))
#         if kwargs.get('sort', None):
#             self.__params.append(('sort', kwargs.get('sort')))
# 
#         self.__rows = kwargs.get('rows', 10)
# 
#         self.response_mapper = kwargs.get('response_mapper', lambda x: x)
#         self.headers = kwargs.get('headers', None)
# 
#     def add_param(self, param):
#         """
#         Directly add a parameter to the `QueryBuilder`. This is intended for
#         stuff like `dismax` parametrization etc.
# 
#         `param` needs to be a tuple of the form::
#             (name, value)
#         """
#         self.__params.append(param)
# 
#     def add_params(self, params):
#         """
#         Directly add a parameter list to the `QueryBuilder`. This is intended
#         for stuff like `dismax` parametrization etc.
# 
#         `params` needs to be a list of tuples of the form::
#             (name, value)
#         """
#         self.__params.extend(params)
# 
#     def add(self, query):
#         """
#         Add a query objet to the `QueryBuilder`.
#         """
#         if not isinstance(query, BaseQuery):
#             raise TypeError
#         if isinstance(query, Query):
#             if self.__has_q:
#                 raise TooManyQs
#             self.__has_q = True
#         self.__queries.append(query)
# 
#     def get_params(self):
#         """
#         Compute and return a list of all query values.
#         """
#         queries = []
#         queries.extend(self.__params)
#         if not self.__has_q:
#             queries.append(('q', '*:*'))
#         has_facet_on = False
#         has_rows = False
#         for q in self.__queries:
#             for name, value in q.get_params():
#                 if name == 'facet':
#                     if not has_facet_on:
#                         has_facet_on = True
#                     else:
#                         continue
#                 if name == 'rows':
#                     has_rows = True
#                 queries.append((name, value))
# 
#         if not has_rows:
#             queries.append(('rows', str(self.__rows)))
#         return queries


# Path: dopplr/solr/client.py
import logging
import json
import urllib
import types
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.httputil import HTTPHeaders
from tornado.ioloop import IOLoop
from .querybuilder import QueryBuilder

        else:
            self._get(final_url, headers=querybuilder.headers,
                callback=handle_search_response(querybuilder, callback))

    def term_suggest(self, querybuilder, callback=None):
        """
        simple query against the /term_suggest requesthandler from solr.
        """
        query_params = self._get_params(querybuilder)

        log.debug('term_suggest with params: %s' % query_params)
        qs = urllib.urlencode(query_params)
        final_url = '?'.join([self._termsuggest_url, qs])
        log.debug('Final suggest URL: %s' % final_url)

        self._get(final_url, headers=querybuilder.headers,
            callback=handle_suggest_response(querybuilder, callback))

    def get_ids(self, ids, fields=None, response_mapper=None, callback=None):
        """
        Using the Solr 4.X realtime /get handler:
        https://wiki.apache.org/solr/RealTimeGet
        """
        log.debug('realtime get with ids: %s' % ids)
        params = [('ids', ','.join(map(unicode, ids)))]
        if fields:
            params.append(('fl', ','.join(fields)))
        qs = urllib.urlencode(params)
        final_url = '?'.join([self._get_url, qs])
        log.debug('Final get URL: %s' % final_url)

        qb = QueryBuilder(response_mapper=(response_mapper or (lambda x: x)))