Exemplo n.º 1
0
    def _buildQuery(self, query, sortq, sort_dict, doc_limit, offsetval):
        """Build and return a search query object."""

        # computed and returned fields examples.  Their use is not required
        # for the application to function correctly.
        computed_expr = search.FieldExpression(name='adjusted_price',
                                               expression='price * 1.08')
        returned_fields = [
            docs.Product.PID, docs.Product.DESCRIPTION, docs.Product.CATEGORY,
            docs.Product.AVG_RATING, docs.Product.PRICE,
            docs.Product.PRODUCT_NAME
        ]

        if sortq == 'relevance':
            # If sorting on 'relevance', use the Match scorer.
            sortopts = search.SortOptions(match_scorer=search.MatchScorer())
            search_query = search.Query(
                query_string=query.strip(),
                options=search.QueryOptions(
                    limit=doc_limit,
                    offset=offsetval,
                    sort_options=sortopts,
                    snippeted_fields=[docs.Product.DESCRIPTION],
                    returned_expressions=[computed_expr],
                    returned_fields=returned_fields))
        else:
            # Otherwise (not sorting on relevance), use the selected field as the
            # first dimension of the sort expression, and the average rating as the
            # second dimension, unless we're sorting on rating, in which case price
            # is the second sort dimension.
            # We get the sort direction and default from the 'sort_dict' var.
            if sortq == docs.Product.AVG_RATING:
                expr_list = [
                    sort_dict.get(sortq),
                    sort_dict.get(docs.Product.PRICE)
                ]
            else:
                expr_list = [
                    sort_dict.get(sortq),
                    sort_dict.get(docs.Product.AVG_RATING)
                ]
            sortopts = search.SortOptions(expressions=expr_list)
            # logging.info("sortopts: %s", sortopts)
            search_query = search.Query(
                query_string=query.strip(),
                options=search.QueryOptions(
                    limit=doc_limit,
                    offset=offsetval,
                    sort_options=sortopts,
                    snippeted_fields=[docs.Product.DESCRIPTION],
                    returned_expressions=[computed_expr],
                    returned_fields=returned_fields))
        return search_query
Exemplo n.º 2
0
  def get(self, terms):
    self.response.headers['Access-Control-Allow-Origin'] = '*'
    scoring = self.request.get('noscore', None) is None
    include_results = self.request.get('noresults', None) is None
    include_count = self.request.get('count', None) is not None
    request_cursor = self.request.get('cursor', None)

    if not include_results:
      scoring = False
      include_count = True
    try:
      limit = min(20, int(self.request.get('limit', 20)))
    except ValueError:
      self.response.set_status(400)
      return
    index = search.Index('repo')
    cursor = search.Cursor(web_safe_string=request_cursor)
    try:
      # Accuracy refers to accurate till n results.
      accuracy = 2000 if include_count else None
      sort_options = search.SortOptions(match_scorer=search.MatchScorer()) if scoring else None
      query_options = search.QueryOptions(limit=limit, number_found_accuracy=accuracy, sort_options=sort_options, cursor=cursor)
      search_results = index.search(search.Query(query_string=terms, options=query_options))
      cursor = search_results.cursor
    except search.QueryError:
      self.response.set_status(400)
      self.response.write('bad query')
      return

    count = search_results.number_found
    if include_results:
      result_futures = []
      for result in search_results.results:
        (owner, repo) = result.doc_id.split('/')
        version = None
        for field in result.fields:
          if field.name == 'version':
            version = field.value
            break
        library_key = ndb.Key(Library, Library.id(owner, repo))
        result_futures.append(LibraryMetadata.brief_async(library_key, version, assume_latest=True))
      results = []
      for future in result_futures:
        result = yield future
        if result is None:
          # Fixup count when we skip over incomplete entries.
          count = count - 1
        if result is not None:
          results.append(result)

    result = {
        'cursor': cursor.web_safe_string if cursor and include_results else None,
    }
    if include_count:
      result['count'] = count
    if include_results:
      result['results'] = results

    self.response.headers['Content-Type'] = 'application/json'
    self.response.write(json.dumps(result))
Exemplo n.º 3
0
    def post(self):
        if self.useradmin:
            # for searching
            query = self.request.get('search').strip()
            if query:
                # sort results by date descending
                expr_list = [
                    search.SortExpression(
                        expression='date',
                        default_value=datetime(1999, 01, 01),
                        direction=search.SortExpression.DESCENDING)
                ]
                # construct the sort options
                sort_opts = search.SortOptions(expressions=expr_list)
                query_options = search.QueryOptions(
                    limit=10,
                    snippeted_fields=['content'],
                    sort_options=sort_opts,
                    returned_fields=['path_link'])

                query_obj = search.Query(query_string=query,
                                         options=query_options)
                results = search.Index(name=_INDEX_NAME).search(
                    query=query_obj)
                len_results = len(results.results)

                self.render('internalhome.html',
                            results=results,
                            len_results=len_results,
                            query=query)
Exemplo n.º 4
0
    def get(self):
        lat = int(self.request.get('lat'))
        lon = int(self.request.get('lon'))
        offset = int(self.request.get('offset', 0))
        limit = int(self.request.get('limit', 9))

        self.response.headers['Content-Type'] = 'application/json'
        index = search.Index(name='photo')
        user_location = (lat, lon)

        query = "distance(geopoint, geopoint(%f, %f)) >= %f" % (
            user_location[0], user_location[1], 0)
        loc_expr = "distance(geopoint, geopoint(%f, %f))" % (user_location[0],
                                                             user_location[1])
        sortexpr = search.SortExpression(
            expression=loc_expr,
            direction=search.SortExpression.ASCENDING,
            default_value=45001)
        search_query = search.Query(
            query_string=query,
            options=search.QueryOptions(
                limit=limit,
                offset=offset,
                sort_options=search.SortOptions(expressions=[sortexpr])))
        photos = index.search(search_query)

        photos_data = []
        for photo in photos:
            photo_data = {
                'url': photo.field('url').value,
                'stream_id': photo.field('stream_id').value
            }
            photos_data.append(photo_data)

        self.response.out.write(json.dumps(photos_data))
Exemplo n.º 5
0
def get_search_suggestions(searchstring):
    logging.info("search string was: " + searchstring)
    # get the search string
    query= searchstring
    #reg_ex = searchstring+ ".*"
    reg_ex = re.compile(searchstring + ".*")
    # create the query object
    sort_expression = [search.SortExpression(expression='rank', 
        direction = search.SortExpression.DESCENDING)]
    sort_opt = search.SortOptions(expressions=sort_expression)
    query_options = search.QueryOptions(limit = 20, sort_options=sort_opt)
    query_obj = search.Query(query_string=query, options=query_options)
    results = search.Index(name=_INDEX_NAME).search(query=query_obj)

    logging.info(results)
    # we need to limit the suggestion to at 20 possible options
    # sorted alphabetically
    possibilities = []
    temp_tags = []
    for result in results:
        for field in result.fields:
            if field.name == "stream_name":
                possibilities.append(field.value)
            if field.name == "tags" and field.value is not None:
                temp_tags = field.value.split(" ")
                possibilities.extend(temp_tags)
    

    possibilities = [x for x in possibilities if x.startswith(searchstring)]
    sorted_possibilities = sorted(possibilities)
    
    logging.info(sorted_possibilities)
    return sorted_possibilities[:20]
    def search_query(self, query_string, page=0):

        # Create sort options to sort on price and brand.
        sort_ts = search.SortExpression(
            expression='ts',
            direction=search.SortExpression.DESCENDING,
            default_value=0)
        sort_options = search.SortOptions(expressions=[sort_ts])

        # Create query options using the sort options and expressions created
        # above.
        query_options = search.QueryOptions(limit=self.MSG_PER_PAGE_NUM,
                                            offset=page *
                                            self.MSG_PER_PAGE_NUM,
                                            returned_fields=['msg_key'],
                                            sort_options=sort_options)

        # Build the Query and run the search
        try:
            query = search.Query(query_string=query_string,
                                 options=query_options)
        except search.QueryError:
            return []
        results = self.index.search(query)

        return results
Exemplo n.º 7
0
def query_options():
    index = search.Index('products')
    query_string = "product: piano AND price < 5000"

    # Create sort options to sort on price and brand.
    sort_price = search.SortExpression(
        expression='price',
        direction=search.SortExpression.DESCENDING,
        default_value=0)
    sort_brand = search.SortExpression(
        expression='brand',
        direction=search.SortExpression.DESCENDING,
        default_value="")
    sort_options = search.SortOptions(expressions=[sort_price, sort_brand])

    # Create field expressions to add new fields to the scored documents.
    price_per_note_expression = search.FieldExpression(name='price_per_note',
                                                       expression='price/88')
    ivory_expression = search.FieldExpression(
        name='ivory', expression='snippet("ivory", summary, 120)')

    # Create query options using the sort options and expressions created
    # above.
    query_options = search.QueryOptions(
        limit=25,
        returned_fields=['model', 'price', 'description'],
        returned_expressions=[price_per_note_expression, ivory_expression],
        sort_options=sort_options)

    # Build the Query and run the search
    query = search.Query(query_string=query_string, options=query_options)
    results = index.search(query)
    for scored_document in results:
        print(scored_document)
Exemplo n.º 8
0
def create_sort_options(fields, default_values=None):
    default_values = default_values or {}
    expressions = []

    fields = fields if isinstance(fields, (list, tuple)) else [fields]

    for field in fields:
        if isinstance(field, search_api.SortExpression):
            expressions.append(field)
            continue

        if field.startswith('-'):
            field = field[1:]
            direction_exp = search_api.SortExpression.DESCENDING
        else:
            direction_exp = search_api.SortExpression.ASCENDING

        default_value = default_values.get(field, '')

        if inspect.isfunction(default_value):
            default_value = default_value(field, direction_exp)

        expressions.append(
            search_api.SortExpression(expression=field,
                                      direction=direction_exp,
                                      default_value=default_value))

    return search_api.SortOptions(expressions=expressions)
Exemplo n.º 9
0
    def get(self):
        validate(self, 'editor')
        search_phrase = ""
        if self.request.get('search_phrase'):
            search_phrase = self.request.get('search_phrase')
        sort_opts = search.SortOptions()
        # we should consider adding source to the series data
        search_phrase_options = search.QueryOptions(
            limit=10,
            sort_options=sort_opts,
            returned_fields=['series_type'],
            #snippeted_fields=['description','source']
        )

        search_phrase_obj = search.Query(query_string=search_phrase +
                                         " series_type : series",
                                         options=search_phrase_options)
        results = search.Index(name=_INDEX_NAME).search(
            query=search_phrase_obj)
        cursor_string, table_of_results = create_table_of_results(results)
        data_display_order = [u'series_type', u'description', u'source']
        template_values = login_status(self.request.uri)
        template_values.update({
            'search_phrase': search_phrase,
            'results': table_of_results,
            'data_display_order': data_display_order,
            'cursor_string': cursor_string
        })
        template = jinja_environment.get_template('choose_series.html')
        self.response.out.write(template.render(template_values))
Exemplo n.º 10
0
    def get(self):
        """Handles a get request with a query."""
        uri = urlparse(self.request.uri)
        query = ''
        if uri.query:
            query = parse_qs(uri.query)
            query = query['query'][0]

        # sort results by author descending
        expr_list = [
            search.SortExpression(expression='author',
                                  default_value='',
                                  direction=search.SortExpression.DESCENDING)
        ]
        # construct the sort options
        sort_opts = search.SortOptions(expressions=expr_list)
        query_options = search.QueryOptions(limit=3, sort_options=sort_opts)
        query_obj = search.Query(query_string=query, options=query_options)
        results = search.Index(name=_INDEX_NAME).search(query=query_obj)
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'results': results,
            'number_returned': len(results.results),
            'url': url,
            'url_linktext': url_linktext,
        }
        self.render_template('index.html', template_values)
Exemplo n.º 11
0
def get_search_results(query, page=0):
    assert page >= 0
    assert page < 20
    extra = None

    expr_list = [
        search.SortExpression(expression='author',
                              default_value='',
                              direction=search.SortExpression.DESCENDING)
    ]

    sort_opts = search.SortOptions(expressions=expr_list)
    query_options = search.QueryOptions(limit=30, sort_options=sort_opts)
    query_obj = search.Query(query_string=query, options=query_options)

    results_posts = search.Index(name=_INDEX_SEARCH).search(query=query_obj)
    results = []
    for result in results_posts:
        a = Post.get_by_id(long(result.doc_id))
        results.append(a)

    if len(results) > PAGE_SIZE:
        if page < 19:
            extra = results[-1]
        quotes = results[:PAGE_SIZE]

    return results, extra
Exemplo n.º 12
0
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        s = self.request.get('s')
        limit = self.request.get('limit', 5)
        offset = self.request.get('offset', 0)

        s = urllib.unquote_plus(s)

        query = '"{0}"'.format(s.replace('"', ''))
        stream_list = []

        expr_list = [
            search.SortExpression(expression='name',
                                  default_value='',
                                  direction=search.SortExpression.DESCENDING),
            search.SortExpression(expression='tags',
                                  default_value='',
                                  direction=search.SortExpression.DESCENDING)
        ]
        # construct the sort options
        sort_opts = search.SortOptions(expressions=expr_list)
        query_options = search.QueryOptions(limit=int(limit),
                                            offset=int(offset),
                                            sort_options=sort_opts)
        query_obj = search.Query(query_string=query, options=query_options)
        results = search.Index(name='stream').search(query=query_obj)

        for result in results:
            stream_list.append({
                'id': result.field('stream_id').value,
                'name': result.field('name').value,
                'cover_url': result.field('cover_url').value
            })

        self.response.out.write(json.dumps(stream_list))
Exemplo n.º 13
0
    def _geo_search(self, lat=None, lng=None, search_radius_meters=5000):
        """
        Return plaques within search_radius_meters of lat/lng, sorted by
        distance from lat/lng.
        """
        search_radius_meters = int(search_radius_meters)
        plaque_search_index = search.Index(PLAQUE_SEARCH_INDEX_NAME)

        loc_expr = 'distance(location, geopoint({}, {}))'.format(lat, lng)
        query = '{} < {}'.format(loc_expr, search_radius_meters)
        sortexpr = search.SortExpression(
            expression=loc_expr,
            direction=search.SortExpression.ASCENDING,
            default_value=search_radius_meters)

        search_query = search.Query(
            query_string=query,
            options=search.QueryOptions(sort_options=search.SortOptions(
                expressions=[sortexpr])))

        results = plaque_search_index.search(search_query)
        keys = [ndb.Key(urlsafe=r.doc_id) for r in results]
        raw_plaques = ndb.get_multi(keys)
        plaques = [p for p in raw_plaques if p is not None and p.approved]
        return plaques
Exemplo n.º 14
0
    def get(self):
        uri = urlparse(self.request.uri)
        query = 'null'
        if uri.query:
            query = parse_qs(uri.query)
            query = query['query'][0]

        if query is not 'null':
            expr_list = [search.SortExpression(
                expression='author', default_value='',
                direction=search.SortExpression.DESCENDING)]

            sort_opts = search.SortOptions(expressions=expr_list)
            query_options = search.QueryOptions(limit=9, sort_options=sort_opts)
            query_obj = search.Query(query_string=query, options=query_options)

            results_firms = search.Index(name=_INDEX_NAME_FIRMS).search(query=query_obj)

            self.render('search.html', {
                'user': self.current_user,
                'title': "Search - Equilibrium",
                'results_firms': results_firms,
                'images': images.get_serving_url,
                'len': results_firms.number_found
            })
        else:
            self.render('search.html', {
                'user': self.current_user,
                'title': "Search - Equilibrium"
            })
Exemplo n.º 15
0
    def _run_query(self):
        offset = self._offset
        limit = self._limit
        sort_expressions = self._sorts

        if self._raw_query is not None:
            query_string = self._raw_query
        else:
            query_string = str(self.query)

        kwargs = {"expressions": sort_expressions}
        if self._match_scorer:
            kwargs["match_scorer"] = self._match_scorer

        snippet_words = self.get_snippet_words()
        field_expressions = self.get_snippet_expressions(snippet_words)

        sort_options = search_api.SortOptions(**kwargs)
        search_options = search_api.QueryOptions(
            offset=offset,
            limit=limit,
            sort_options=sort_options,
            ids_only=self.ids_only,
            number_found_accuracy=100,
            returned_expressions=field_expressions,
        )
        search_query = search_api.Query(query_string=query_string,
                                        options=search_options)

        self._results_response = self.index.search(search_query)
        self._number_found = self._results_response.number_found
Exemplo n.º 16
0
class AdminHome(basehandler.BaseHandler):
    """
    Handles the admin view page /admin/admin_home.html
    - The admin home has the search feature for searching text in all wikis published
    """
    def get(self):
        self.render('admin/admin_home.html')

    def post(self):
        query = self.request.get('search').strip()
        if query:
            # sort results by date descending
            expr_list = [search.SortExpression(
                expression='date', default_value=datetime(1999, 01, 01),
                direction=search.SortExpression.DESCENDING)]
            # construct the sort options
            sort_opts = search.SortOptions(
                expressions=expr_list)
            query_options = search.QueryOptions(
                limit = 10,
                snippeted_fields=['content'],
                sort_options=sort_opts,
                returned_fields = ['path_link'])

            query_obj = search.Query(query_string=query, options=query_options)
            results = search.Index(name = config.__INDEX_NAME__).search(query=query_obj)
            len_results = len(results.results)

            self.render('admin/admin_home.html', results = results,
                        len_results = len_results, query = query)
Exemplo n.º 17
0
	def get(self, args):
		template_vals = generate_base_template_vals(self)
		template_vals['title'] = 'Members'
		template_vals['page'] = 'members'
		
		# Check for a search query.
		search_query = self.request.get('q')
		
		# If there was no search or semester specified, default to the current semester.
		if not search_query:
			search_query = 'semester:' + str(get_current_semester())
		
		# Run the search.
		search_results = search.Index(MEMBER_SEARCH_INDEX_NAME).search(search.Query(
				query_string=search_query,
				options=search.QueryOptions(
					limit=999,
					ids_only=True,
					sort_options=search.SortOptions(
						expressions=[
							search.SortExpression(
								expression='name',
								direction=search.SortExpression.ASCENDING, 
								default_value='')
						]))))
		
		# Fetch the members.
		show_private = users.is_current_user_admin()
		members = []
		for result in search_results.results:
			member = Member.query(Member.id == result._doc_id).get()
			if member and (member.show or show_private):
				members.append(member)
		
		template_vals['members'] = members
		
		# Get all possible semesters to put in the menu.
		selected_semester = get_semester_from_query(search_query)
		
		semesters = []
		for semester in get_all_semesters():
			semesters.append({
				'id': semester,
				'pretty': semester_pretty(semester),
				'selected': semester == selected_semester
			})
		template_vals['semesters'] = semesters
		if selected_semester and selected_semester > FIRST_SEMESTER:
			template_vals['prev_semester'] = prev_semester(selected_semester)
		if selected_semester and selected_semester < get_current_semester():
			template_vals['next_semester'] = next_semester(selected_semester)
		if selected_semester and selected_semester >= FIRST_SEMESTER and selected_semester <= get_current_semester():
			template_vals['selected_semester'] = selected_semester
		
		template = JINJA_ENVIRONMENT.get_template('members_list.html')
		self.response.write(template.render(template_vals))
 def createQuery(self, search_str):
     query_str = self.getQueryString(search_str)
     sort = search.SortExpression(expression='name', direction=search.SortExpression.ASCENDING)
     sort_opts = search.SortOptions(expressions=[sort])
     query_options = search.QueryOptions(
         limit=20,
         sort_options=sort_opts
     )
     query = search.Query(query_string=query_str, options=query_options)
     return query
Exemplo n.º 19
0
    def post(self):
        try:
            q = self.request.get('q')
            include = self.request.get('include')
            logging.info("Searching for " + q)
            query = "(name:%s OR html:%s)" % (q, q)
            book = self.request.get('book_filter')
            query = "book:%s AND %s" % (book,
                                        query) if book is not None and len(
                                            book.strip()) > 0 else query
            sort_opts = search.SortOptions(match_scorer=search.MatchScorer())
            opts = search.QueryOptions(limit=100,
                                       snippeted_fields=['html'],
                                       sort_options=sort_opts)
            results = []
            for indexName in ["private", "public"]:
                if include is not None and len(
                        include.strip()) > 0 and include.find(indexName) == -1:
                    results.append({'count': -1, 'results': [], 'show': False})
                    continue
                index_results = []
                index = search.Index(indexName)
                active_q = "owners:%s AND %s" % (get_current_session().get(
                    "account"), query) if indexName == "private" else query
                search_query = search.Query(query_string=active_q,
                                            options=opts)
                search_results = index.search(search_query)
                for doc in search_results:
                    internal = db.get(doc.doc_id)
                    if internal is not None:
                        logging.info("Got expressions %s" % doc.expressions)
                        index_results.append({
                            "snippets": doc.expressions,
                            "internal": internal
                        })
                results.append({
                    'count': search_results.number_found,
                    'results': index_results,
                    'show': True
                })

            template_values = {
                "current_user": get_current_session().get("account"),
                "private_results": results[0]['results'],
                "private_count": results[0]['count'],
                "private_show": results[0]['show'],
                "public_results": results[1]['results'],
                "public_count": results[1]['count'],
                "public_show": results[1]['show']
            }
            path = os.path.join(os.path.dirname(__file__),
                                'html/search_results.html')
            self.response.out.write(template.render(path, template_values))
        except search.Error:
            respondWithMessage(self, "Search error")
Exemplo n.º 20
0
def create_sort_options(field, direction='asc', default_value=None):
    direction_exp = search_api.SortExpression.ASCENDING if direction == 'asc' else search_api.SortExpression.DESCENDING

    if inspect.isfunction(default_value):
        default_value = default_value(field, direction)

    return search_api.SortOptions(expressions=[
        search_api.SortExpression(expression=field,
                                  direction=direction_exp,
                                  default_value=default_value or '')
    ])
Exemplo n.º 21
0
def find(query, count=10):
    sort_opts = search.SortOptions(expressions=[
        search.SortExpression(expression="date",
                              direction=search.SortExpression.ASCENDING)
    ])
    query_options = search.QueryOptions(limit=count, sort_options=sort_opts)
    query_obj = search.Query(query_string=query, options=query_options)
    results = index.search(query=query_obj)
    keys = []
    for result in results:
        keys.append(result.doc_id)
    return keys
Exemplo n.º 22
0
def search_node_orders(query=None, page_size=20, cursor=None):
    # type: (unicode, int, unicode) -> tuple[list[NodeOrder], search.Cursor, bool]
    options = search.QueryOptions(limit=page_size,
                                  cursor=search.Cursor(cursor),
                                  ids_only=True,
                                  sort_options=search.SortOptions(
                                      expressions=[SortExpression(expression='order_time',
                                                                  direction=SortExpression.DESCENDING)]))
    search_results = NODE_ORDER_INDEX.search(search.Query(query, options=options))  # type: search.SearchResults
    results = search_results.results  # type: list[search.ScoredDocument]
    node_orders = ndb.get_multi([NodeOrder.create_key(long(result.doc_id)) for result in results])
    return node_orders, search_results.cursor, search_results.cursor is not None
Exemplo n.º 23
0
    def search(cls, query):
        sort1 = search.SortExpression(expression='created', direction=search.SortExpression.DESCENDING)
        sort_options = search.SortOptions(expressions=(sort1,))

        index = search.Index(name="user")
        results = index.search(query=search.Query(
            query,
            options = search.QueryOptions(
                number_found_accuracy=1000,
                sort_options=sort_options,
            ),
        ))
        return results
Exemplo n.º 24
0
def index_search_query_data(master_code, query_string, page_size=100, max_result=10000, offset=0):
  index = get_master_search_index(master_code)
  # sort option
  sort_expression = search.SortExpression(
        expression='data_key',
        direction=search.SortExpression.ASCENDING,
        default_value='')
  sort = search.SortOptions(expressions=[sort_expression], limit=max_result)
  q_ft = search.Query(query_string=query_string, options=search.QueryOptions(sort_options=sort, limit=page_size, offset=offset, ids_only=True))
  results = index.search(q_ft)
  logging.info('operator.number_found=' + str(results.number_found))

  return results, results.number_found
Exemplo n.º 25
0
 def search_index_suggest(self, user_ip, status, **params):
   logger.debug('Suggest Search Param Results: ' + str(params))
   #locality = params['address.locality']   #TODO: Hardcoded for testing. To be made generic.    
   
   suggest_playgrounds = []
   if status != 'all':
     status_value = STATUS_DICT.get(status)
     logger.debug('Status %d ' % (status_value))
     
   query_str = 'status:'+str(status_value)
     
   for key, value in params.items():      
     if key == 'latlong' and value is not None:
       query_str += ' AND distance(latlong, geopoint('+str(value)+')) < 5000'
     if '.' in key and value is not None:
       struct, attr = key.split('.')
       if struct == 'address':          
         if attr == 'locality':
           query_str += ' NOT locality:'+str(value)
           
   try:
     index = search.Index(PLAYGROUND)          
     
     sortopts = search.SortOptions(expressions=[
         search.SortExpression(expression='name', direction='ASCENDING')])
     search_query = search.Query(
         query_string=query_str,
         options=search.QueryOptions(
             limit=PAGE_SIZE,
             sort_options=sortopts))
     search_results = index.search(search_query)
     #logger.debug('Suggest Search Result:' + str(search_results))
     
   except search.Error:
     logger.exception("NdbPlaygroundDao:: Search query failed for suggest playgrounds")
     #Retrieve the doc_id from the search results and then use that to query the datastore to fetch the entity
   
   keys = []    
   for doc in search_results:
     keys.append(ndb.Key(Playground, long(doc.doc_id)))
   
   #suggest_playgrounds = ndb.get_multi(keys)    
   #return suggest_playgrounds
   cache_id = 'suggest_'+str(PLAYGROUND)+'_'+str(user_ip)
   get_keys = mc_get(cache_keys.get_suggest_keys_cache_key(cache_id))    
   if get_keys is not None:
     del_keys = mc_delete(cache_keys.get_suggest_keys_cache_key(cache_id))
   add_keys = mc_wrap(cache_keys.get_suggest_keys_cache_key(cache_id), ENTITY_CACHE_EXPIRATION,
                     lambda x: keys)
   logger.info('No of Suggest Playground Added to cache : %s' % len(add_keys))
   return keys
Exemplo n.º 26
0
    def get_posts(self, keyword=None, index=0):
        """
        Get posts with keyword.
        :param keyword: search keyword
        :param index: pager index
        :return: [post], total
        """
        query_string = ''
        if keyword and len(keyword.strip()) > 0:
            source = [item for item in keyword.split(' ') if len(item) > 0]
            plus = [item for item in source if item.find('-') != 0]
            minus = [item[1:] for item in source if item.find('-') == 0 and len(item) > 1]

            if len(plus) > 0:
                keyword = ' '.join(plus)
                query_string = '((title:{1}) OR (content:{1}))'.replace('{1}', keyword)
            if len(minus) > 0:
                keyword = ' '.join(minus)
                query_string = 'NOT ((title:{1}) OR (content:{1}))'.replace('{1}', keyword)

        create_time_desc = search.SortExpression(
            expression = 'create_time',
            direction = search.SortExpression.DESCENDING,
            default_value = '0')
        options = search.QueryOptions(
            offset = config.page_size * index,
            limit = config.page_size,
            sort_options = search.SortOptions(expressions=[create_time_desc]),
            returned_fields = ['title', 'content', 'author', 'create_time'])
        query = search.Query(query_string, options=options)
        try:
            documents = search.Index(name=config.text_search_name).search(query)
        except: # schema missing
            return [], 0

        result = []
        for document in documents:
            result.append({'doc_id': document.doc_id,
                           'title': document.field('title').value,
                           'content': document.field('content').value,
                           'author': document.field('author').value,
                           'deletable': g.user and (g.user['email'] == document.field('author').value or g.user['is_admin']),
                           'create_time': document.field('create_time').value.strftime('%Y-%m-%dT%H:%M:%S.%fZ')})

        # if number of documents over maximum then return the maximum
        if documents.number_found > 1000 + config.page_size:
            count = 1000 + config.page_size
        else:
            count = documents.number_found

        return result, count
Exemplo n.º 27
0
def fsearch(query_string, kind, limit=40, offset=0):
    """Suche nach Ersatzteilen und WebProducts"""

    expressions = [search.SortExpression(expression='aktiv',
                                         direction=search.SortExpression.DESCENDING),
                   search.SortExpression(expression='ausgegangen',
                                         direction=search.SortExpression.ASCENDING)]
    sort_options = search.SortOptions(expressions=expressions)

    options = search.QueryOptions(limit=limit,
                                  sort_options=sort_options,
                                  offset=offset)
    query_string = 'kind:%s %s' % (kind, query_string)
    hits, _cursor, total = perform_search(INDEX_NAME, query_string, options)
    return hits, total
Exemplo n.º 28
0
def get_query_options(query_dict, limit=10):
    if 'lat' in query_dict and 'lon' in query_dict:
        print 'lat ' + str(query_dict['lat'])
        print 'lon ' + str(query_dict['lon'])
        loc_expr = "distance(place, geopoint(%f, %f))" % (query_dict['lon'],
                                                          query_dict['lat'])
        sortexpr = search.SortExpression(
            expression=loc_expr,
            direction=search.SortExpression.ASCENDING,
            default_value=45001)
        return search.QueryOptions(
            limit=limit,
            sort_options=search.SortOptions(expressions=[sortexpr]))
    else:
        return search.QueryOptions(limit=limit)
Exemplo n.º 29
0
def search_user(name, admin=False):
    query_str = 'name = %s' % name if not admin else 'name = %s AND group: ADMIN' % name
    # Build the SortOptions with 2 sort keys
    sort1 = search.SortExpression(expression='name',
                                  direction=SortExpression.ASCENDING)
    sort_opts = search.SortOptions(expressions=[sort1])

    # Build the QueryOptions
    # Create a FieldExpression
    expr2 = search.FieldExpression(name='name_snippet',
                                   expression='snippet("%s", name, 20)' % name)
    options = search.QueryOptions(sort_options=sort_opts,
                                  returned_expressions=[expr2])
    query = search.Query(query_str, options)
    return user_index.search(query)
Exemplo n.º 30
0
def search_tag_index_alpha(search_string, limit):
    index = get_tag_index()
    search_results = index.search(query=search.Query(
        "string: {}".format(search_string),
        options=search.QueryOptions(
            limit=limit,
            sort_options=search.SortOptions(expressions=[
                search.SortExpression(expression='id', default_value='')
            ],
                                            limit=1000),
            returned_fields=['id'])))
    tags = set()
    for res in search_results:
        tags.add(res.fields[0].value)
    return list(tags)