def get_solr_query_string(self, query): """ Parses the query and returns a query string. The fields must be replaced by the internal field name given. Args: query: The query string. Returns: A SOLR string. """ query_string = "q={0}{1}{2}".format(Document.INDEX_NAME, COLON, self.__index.name) if len(query) > 0: query = urllib.unquote(query) query = query.strip() if not isinstance(query, unicode): query = unicode(query, 'utf-8') logging.debug(u"Query: {0}".format(query)) query_tree = query_parser.ParseAndSimplify(query) logging.debug(u"Tree dump:{0}".format( self.__dump_tree(query_tree))) query_string += "+AND+" + self.__create_query_string(query_tree) logging.debug(u"Query string {0}".format(query_string)) # Use edismax as the parsing engine for more query abilities. query_string += "&defType=edismax" query_fields = self.__get_query_fields() # Get the field list for the query. field_list = self.__get_field_list() logging.debug(u"Field list: {0}".format(field_list)) if field_list: query_string += field_list else: logging.debug(u"Using default field list") query_string += "&fl=id+" + Document.INDEX_LOCALE + "+" + query_fields # Set sort order. query_string += self.__get_sort_list() # Restrict to only fields requested or all of the fields from the schema. query_string += "&qf=" + query_fields query_string += "&pf=" + query_fields query_string += self.__get_row_limit() query_string += self.__get_offset() logging.debug(u"SOLR STRING: {0}".format(query_string)) return query_string
def prepare_solr_query(index, gae_query, projection_fields, sort_fields, limit, offset): """ Constructor query parameters dict to be sent to Solr. Args: index: An Index for the query to run. gae_query: A str representing query sent by user. projection_fields: A list of fields to fetch for each document. sort_fields: a list of tuples of form (<FieldName>, "desc"/"asc") limit: a max number of document to return. offset: an integer representing offset. Returns: A dict containing http query params to be sent to Solr. """ params = {} solr_query = '{}:{}'.format(INDEX_NAME_FIELD, index.name) if not isinstance(gae_query, unicode): gae_query = unicode(gae_query, 'utf-8') logging.debug(u'GAE Query: {}'.format(gae_query)) if gae_query: query_tree = query_parser.ParseAndSimplify(gae_query) logging.debug(u'Tree dump: {}'.format(query_tree.toStringTree())) solr_query += ' AND ' + _create_query_string(index.name, query_tree) params['q'] = solr_query # Use edismax as the parsing engine for more query abilities. params['defType'] = 'edismax' # Restrict to only known index fields. search_fields = ['id'] + [field['name'] for field in index.schema] params['qf'] = ' '.join(search_fields) # Get the field list for the query. if projection_fields: fields_list = ['id', INDEX_NAME_FIELD, INDEX_LOCALE_FIELD] + [ '{}_{}'.format(index.name, field_name) for field_name in projection_fields ] params['fl'] = ' '.join(fields_list) # Set sort order. if sort_fields: sort_list = _get_sort_list(index.name, sort_fields) params['sort'] = ','.join(sort_list) params['rows'] = limit params['start'] = offset logging.debug(u'Solr request params: {}'.format(params)) return params
def Search(self, search_request): """Searches the simple index for .""" query = urllib.unquote(search_request.query()) query = query.strip() score = _ScoreRequested(search_request) if not query: docs = [_ScoredDocument(doc, 0.0) for doc in self._documents.values()] else: if not isinstance(query, unicode): query = unicode(query, 'utf-8') query_tree = query_parser.ParseAndSimplify(query) docs = self._Evaluate(query_tree, score=score) docs = self._Sort(docs, search_request, query, score) docs = self._AttachExpressions(docs, search_request) return docs