示例#1
0
    def _calculate_geo(self, request):
        cities = cache.get("website.cities")

        if not cities:
            elastic = ES(settings.SEARCH_HOSTS)
            geo = []

            for city, location in settings.APP_GEO_CITIES.items():
                filters = [
                    QueryFilter(
                        StringQuery(city, search_fields=["city"], analyze_wildcard=True, default_operator="AND")
                    ),
                    GeoDistanceFilter(
                        "pin.location", {"lat": location[0], "lon": location[1]}, settings.APP_GEO_CITIES_RANGE
                    ),
                ]

                geo.append(
                    (
                        city,
                        elastic.count(FilteredQuery(MatchAllQuery(), ORFilter(filters)), settings.SEARCH_ALIASES)[
                            "count"
                        ],
                        {"lat": location[0], "lon": location[1]},
                    )
                )

            elastic.connection.close()

            data = []
            start_radius, max_radius = (5000, 10000)  # 16km radius
            quotient = None

            for city in sorted(geo, key=lambda student: student[1], reverse=True):
                if city[1] > 0:
                    if not quotient:
                        quotient = int(max_radius / city[1])

                    radius = city[1] * quotient + start_radius
                    data.append((radius,) + city)

            cities = data
            cache.set("website.cities", cities)

        return cities
示例#2
0
def search_people_by_bio(query,
                         limit_results=DEFAULT_LIMIT,
                         index=['onename_people_index']):
    """ queries lucene index to find a nearest match, output is profile username
    """

    from pyes import QueryStringQuery, ES
    conn = ES()

    q = QueryStringQuery(query,
                         search_fields=['username', 'profile_bio'],
                         default_operator='and')

    results = conn.search(query=q, size=20, indices=index)
    count = conn.count(query=q)
    count = count.count

    # having 'or' gives more results but results quality goes down
    if (count == 0):

        q = QueryStringQuery(query,
                             search_fields=['username', 'profile_bio'],
                             default_operator='or')

        results = conn.search(query=q, size=20, indices=index)

    results_list = []
    counter = 0

    for profile in results:

        username = profile['username']
        results_list.append(username)

        counter += 1

        if (counter == limit_results):
            break

    return results_list
def search_people_by_bio(query, limit_results=DEFAULT_LIMIT,
                         index=['onename_people_index']):
    """ queries lucene index to find a nearest match, output is profile username
    """

    from pyes import QueryStringQuery, ES
    conn = ES()

    q = QueryStringQuery(query,
                         search_fields=['username', 'profile_bio'],
                         default_operator='and')

    results = conn.search(query=q, size=20, indices=index)
    count = conn.count(query=q)
    count = count.count

    # having 'or' gives more results but results quality goes down
    if(count == 0):

        q = QueryStringQuery(query,
                             search_fields=['username', 'profile_bio'],
                             default_operator='or')

        results = conn.search(query=q, size=20, indices=index)

    results_list = []
    counter = 0

    for profile in results:

        username = profile['username']
        results_list.append(username)

        counter += 1

        if(counter == limit_results):
            break

    return results_list