当前位置: 首页>>代码示例>>Python>>正文


Python SolrQuery.set_group_field方法代码示例

本文整理汇总了Python中utils.search.solr.SolrQuery.set_group_field方法的典型用法代码示例。如果您正苦于以下问题:Python SolrQuery.set_group_field方法的具体用法?Python SolrQuery.set_group_field怎么用?Python SolrQuery.set_group_field使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在utils.search.solr.SolrQuery的用法示例。


在下文中一共展示了SolrQuery.set_group_field方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: search_prepare_query

# 需要导入模块: from utils.search.solr import SolrQuery [as 别名]
# 或者: from utils.search.solr.SolrQuery import set_group_field [as 别名]
def search_prepare_query(search_query,
                         filter_query,
                         sort,
                         current_page,
                         sounds_per_page,
                         id_weight = DEFAULT_SEARCH_WEIGHTS['id'],
                         tag_weight = DEFAULT_SEARCH_WEIGHTS['tag'],
                         description_weight = DEFAULT_SEARCH_WEIGHTS['description'],
                         username_weight = DEFAULT_SEARCH_WEIGHTS['username'],
                         pack_tokenized_weight = DEFAULT_SEARCH_WEIGHTS['pack_tokenized'],
                         original_filename_weight = DEFAULT_SEARCH_WEIGHTS['original_filename'],
                         grouping = False,
                         include_facets = True,
                         grouping_pack_limit = 1):
    query = SolrQuery()

    field_weights = []
    if id_weight != 0 :
        field_weights.append(("id", id_weight))
    if tag_weight != 0 :
        field_weights.append(("tag", tag_weight))
    if description_weight != 0 :
        field_weights.append(("description", description_weight))
    if username_weight != 0 :
        field_weights.append(("username", username_weight))
    if pack_tokenized_weight != 0 :
        field_weights.append(("pack_tokenized", pack_tokenized_weight))
    if original_filename_weight != 0 :
        field_weights.append(("original_filename", original_filename_weight))

    query.set_dismax_query(search_query,
                           query_fields=field_weights,)
    query.set_query_options(start=(current_page - 1) * sounds_per_page, rows=sounds_per_page, field_list=["id"], filter_query=filter_query, sort=sort)

    if include_facets:
        query.add_facet_fields("samplerate", "grouping_pack", "username", "tag", "bitrate", "bitdepth", "type", "channels", "license")
        query.set_facet_options_default(limit=5, sort=True, mincount=1, count_missing=False)
        query.set_facet_options("tag", limit=30)
        query.set_facet_options("username", limit=30)
        query.set_facet_options("grouping_pack", limit=10)
        query.set_facet_options("license", limit=10)

    if grouping:
        query.set_group_field(group_field="grouping_pack")
        query.set_group_options(group_func=None,
            group_query=None,
            group_rows=10,
            group_start=0,
            group_limit=grouping_pack_limit,  # This is the number of documents that will be returned for each group. By default only 1 is returned.
            group_offset=0,
            group_sort=None,
            group_sort_ingroup=None,
            group_format='grouped',
            group_main=False,
            group_num_groups=True,
            group_cache_percent=0)


    return query
开发者ID:darad,项目名称:freesound,代码行数:61,代码来源:views.py

示例2: tags

# 需要导入模块: from utils.search.solr import SolrQuery [as 别名]
# 或者: from utils.search.solr.SolrQuery import set_group_field [as 别名]
def tags(request, multiple_tags=None):
    if multiple_tags:
        multiple_tags = multiple_tags.split('/')
    else:
        multiple_tags = []
    
    multiple_tags = sorted(filter(lambda x: x, multiple_tags))
    
    try:
        current_page = int(request.GET.get("page", 1))
    except ValueError:
        current_page = 1

    solr = Solr(settings.SOLR_URL)
    query = SolrQuery()
    if multiple_tags:
        query.set_query(" ".join("tag:\"" + tag + "\"" for tag in multiple_tags))
    else:
        query.set_query("*:*")
    query.set_query_options(start=(current_page - 1) * settings.SOUNDS_PER_PAGE, rows=settings.SOUNDS_PER_PAGE, field_list=["id"], sort=["num_downloads desc"])
    query.add_facet_fields("tag")
    query.set_facet_options_default(limit=100, sort=True, mincount=1, count_missing=False)
    query.set_group_field(group_field="grouping_pack")
    query.set_group_options(group_func=None,
        group_query=None,
        group_rows=10,
        group_start=0,
        group_limit=1,
        group_offset=0,
        group_sort=None,
        group_sort_ingroup=None,
        group_format='grouped',
        group_main=False,
        group_num_groups=True,
        group_cache_percent=0,
        group_truncate=True)  # Sets how many results from the same grup are taken into account for computing the facets

    try:
        results = SolrResponseInterpreter(solr.select(unicode(query)))
        paginator = SolrResponseInterpreterPaginator(results, settings.SOUNDS_PER_PAGE)
        num_results = paginator.count
        non_grouped_number_of_results = results.non_grouped_number_of_matches
        page = paginator.page(current_page)
        error = False
        tags = [dict(name=f[0], count=f[1]) for f in results.facets["tag"]]

        docs = results.docs
        resultids = [d.get("id") for d in docs]
        resultsounds = sounds.models.Sound.objects.bulk_query_id(resultids)
        allsounds = {}
        for s in resultsounds:
            allsounds[s.id] = s
        for d in docs:
            d["sound"] = allsounds[d["id"]]

    except SolrException, e:
        error = True
        search_logger.error("SOLR ERROR - %s" % e)
开发者ID:giuband,项目名称:freesound,代码行数:60,代码来源:views.py

示例3: tags

# 需要导入模块: from utils.search.solr import SolrQuery [as 别名]
# 或者: from utils.search.solr.SolrQuery import set_group_field [as 别名]
def tags(request, multiple_tags=None):
    if multiple_tags:
        multiple_tags = multiple_tags.split('/')
    else:
        multiple_tags = []

    multiple_tags = sorted(filter(lambda x: x, multiple_tags))

    try:
        current_page = int(request.GET.get("page", 1))
    except ValueError:
        current_page = 1

    solr = Solr(settings.SOLR_URL)
    query = SolrQuery()
    if multiple_tags:
        query.set_query(" ".join("tag:\"" + tag + "\"" for tag in multiple_tags))
    else:
        query.set_query("*:*")
    query.set_query_options(start=(current_page - 1) * settings.SOUNDS_PER_PAGE, rows=settings.SOUNDS_PER_PAGE, field_list=["id"], sort=["num_downloads desc"])
    query.add_facet_fields("tag")
    query.set_facet_options_default(limit=100, sort=True, mincount=1, count_missing=False)
    query.set_group_field(group_field="grouping_pack")
    query.set_group_options(group_func=None,
                            group_query=None,
                            group_rows=10,
                            group_start=0,
                            group_limit=1,
                            group_offset=0,
                            group_sort=None,
                            group_sort_ingroup=None,
                            group_format='grouped',
                            group_main=False,
                            group_num_groups=True,
                            group_cache_percent=0,
                            group_truncate=True)  # Sets how many results from the same group are taken into account for computing the facets

    page = None
    num_results = 0
    tags = []
    error = False
    docs = {}
    non_grouped_number_of_results = 0
    paginator = None
    try:
        results = SolrResponseInterpreter(solr.select(unicode(query)))
        paginator = SolrResponseInterpreterPaginator(results, settings.SOUNDS_PER_PAGE)
        num_results = paginator.count
        non_grouped_number_of_results = results.non_grouped_number_of_matches
        page = paginator.page(current_page)
        tags = [dict(name=f[0], count=f[1]) for f in results.facets["tag"]]

        docs = results.docs
        resultids = [d.get("id") for d in docs]
        resultsounds = sounds.models.Sound.objects.bulk_query_id(resultids)
        allsounds = {}
        for s in resultsounds:
            allsounds[s.id] = s
        for d in docs:
            d["sound"] = allsounds[d["id"]]

    except SolrException as e:
        error = True
        search_logger.error("SOLR ERROR - %s" % e)
    except:
        error = True

    slash_tag = "/".join(multiple_tags)

    follow_tags_url = ''
    unfollow_tags_url = ''
    show_unfollow_button = False
    if slash_tag:
        follow_tags_url = reverse('follow-tags', args=[slash_tag])
        unfollow_tags_url = reverse('unfollow-tags', args=[slash_tag])
        show_unfollow_button = False

        if request.user.is_authenticated:
            show_unfollow_button = follow_utils.is_user_following_tag(request.user, slash_tag)

    tvars = {'show_unfollow_button': show_unfollow_button,
             'multiple_tags': multiple_tags,
             'follow_tags_url': follow_tags_url,
             'unfollow_tags_url': unfollow_tags_url,
             'error': error,
             'tags': tags,
             'slash_tag': slash_tag,
             'num_results': num_results,
             'non_grouped_number_of_results': non_grouped_number_of_results,
             'docs': docs,
             'paginator': paginator,
             'page': page,
             'current_page': current_page
             }
    return render(request, 'sounds/tags.html', tvars)
开发者ID:MTG,项目名称:freesound,代码行数:97,代码来源:views.py

示例4: search_prepare_query

# 需要导入模块: from utils.search.solr import SolrQuery [as 别名]
# 或者: from utils.search.solr.SolrQuery import set_group_field [as 别名]
def search_prepare_query(search_query,
                         filter_query,
                         sort,
                         current_page,
                         sounds_per_page,
                         id_weight=settings.DEFAULT_SEARCH_WEIGHTS['id'],
                         tag_weight=settings.DEFAULT_SEARCH_WEIGHTS['tag'],
                         description_weight=settings.DEFAULT_SEARCH_WEIGHTS['description'],
                         username_weight=settings.DEFAULT_SEARCH_WEIGHTS['username'],
                         pack_tokenized_weight=settings.DEFAULT_SEARCH_WEIGHTS['pack_tokenized'],
                         original_filename_weight=settings.DEFAULT_SEARCH_WEIGHTS['original_filename'],
                         grouping=False,
                         include_facets=True,
                         grouping_pack_limit=1,
                         offset=None):
    query = SolrQuery()

    # Set field weights and scoring function
    field_weights = []
    if id_weight != 0:
        field_weights.append(("id", id_weight))
    if tag_weight != 0:
        field_weights.append(("tag", tag_weight))
    if description_weight != 0:
        field_weights.append(("description", description_weight))
    if username_weight != 0:
        field_weights.append(("username", username_weight))
    if pack_tokenized_weight != 0:
        field_weights.append(("pack_tokenized", pack_tokenized_weight))
    if original_filename_weight != 0:
        field_weights.append(("original_filename", original_filename_weight))
    query.set_dismax_query(search_query,
                           query_fields=field_weights,)

    # Set start and rows parameters (offset and size)
    if not offset:
        start = (current_page - 1) * sounds_per_page
    else:
        start = offset

    # Process filter
    filter_query = search_process_filter(filter_query)

    # Set all options
    query.set_query_options(start=start, rows=sounds_per_page, field_list=["id"], filter_query=filter_query, sort=sort)

    # Specify query factes
    if include_facets:
        query.add_facet_fields("samplerate", "grouping_pack", "username", "tag", "bitrate", "bitdepth", "type", "channels", "license")
        query.set_facet_options_default(limit=5, sort=True, mincount=1, count_missing=False)
        query.set_facet_options("type", limit=len(sounds.models.Sound.SOUND_TYPE_CHOICES))
        query.set_facet_options("tag", limit=30)
        query.set_facet_options("username", limit=30)
        query.set_facet_options("grouping_pack", limit=10)
        query.set_facet_options("license", limit=10)

    # Add groups
    if grouping:
        query.set_group_field(group_field="grouping_pack")
        query.set_group_options(
            group_func=None,
            group_query=None,
            group_rows=10,
            group_start=0,
            group_limit=grouping_pack_limit,  # This is the number of documents that will be returned for each group. By default only 1 is returned.
            group_offset=0,
            group_sort=None,
            group_sort_ingroup=None,
            group_format='grouped',
            group_main=False,
            group_num_groups=True,
            group_cache_percent=0)
    return query
开发者ID:MTG,项目名称:freesound,代码行数:75,代码来源:views.py

示例5: search_forum

# 需要导入模块: from utils.search.solr import SolrQuery [as 别名]
# 或者: from utils.search.solr.SolrQuery import set_group_field [as 别名]
def search_forum(request):
    search_query = request.GET.get("q", "")
    filter_query = request.GET.get("f", "")
    try:
        current_page = int(request.GET.get("page", 1))
    except ValueError:
        current_page = 1
    current_forum_name_slug = request.GET.get("forum", "").strip()    # for context sensitive search
    if current_forum_name_slug:
        current_forum = get_object_or_404(forum.models.Forum.objects, name_slug=current_forum_name_slug)
    else:
        current_forum = None
    sort = ["thread_created desc"]

    # Parse advanced search options
    advanced_search = request.GET.get("advanced_search", "")
    date_from = request.GET.get("dt_from", "")
    try:
        df_parsed = datetime.datetime.strptime(date_from, "%Y-%m-%d")
        date_from_display = df_parsed.strftime("%d-%m-%Y")
    except ValueError:
        date_from = ""
        date_from_display = "Choose a Date"
    date_to = request.GET.get("dt_to", "")
    try:
        dt_parsed = datetime.datetime.strptime(date_to, "%Y-%m-%d")
        date_to_display = dt_parsed.strftime("%d-%m-%Y")
    except ValueError:
        date_to = ""
        date_to_display = "Choose a Date"

    if search_query.startswith("search in"):
        search_query = ""

    error = False
    error_text = ""
    paginator = None
    num_results = None
    page = None
    results = []
    if search_query.strip() != "" or filter_query:
        # add current forum
        if current_forum:
            filter_query += "forum_name_slug:" + current_forum.name_slug

        # add date range
        if advanced_search == "1" and date_from != "" or date_to != "":
            filter_query = __add_date_range(filter_query, date_from, date_to)

        query = SolrQuery()
        query.set_dismax_query(search_query, query_fields=[("thread_title", 4),
                                                           ("post_body", 3),
                                                           ("thread_author", 3),
                                                           ("post_author", 3),
                                                           ("forum_name", 2)])
        query.set_highlighting_options_default(field_list=["post_body"],
                                               fragment_size=200,
                                               alternate_field="post_body",  # TODO: revise this param
                                               require_field_match=False,
                                               pre="<strong>",
                                               post="</strong>")
        query.set_query_options(start=(current_page - 1) * settings.SOUNDS_PER_PAGE,
                                rows=settings.SOUNDS_PER_PAGE,
                                field_list=["id",
                                            "forum_name",
                                            "forum_name_slug",
                                            "thread_id",
                                            "thread_title",
                                            "thread_author",
                                            "thread_created",
                                            "post_body",
                                            "post_author",
                                            "post_created",
                                            "num_posts"],
                                filter_query=filter_query,
                                sort=sort)

        query.set_group_field("thread_title_grouped")
        query.set_group_options(group_limit=30)

        solr = Solr(settings.SOLR_FORUM_URL)

        try:
            results = SolrResponseInterpreter(solr.select(unicode(query)))
            paginator = SolrResponseInterpreterPaginator(results, settings.SOUNDS_PER_PAGE)
            num_results = paginator.count
            page = paginator.page(current_page)
            error = False
        except SolrException as e:
            logger.warning("search error: query: %s error %s" % (query, e))
            error = True
            error_text = 'There was an error while searching, is your query correct?'
        except Exception as e:
            logger.error("Could probably not connect to Solr - %s" % e)
            error = True
            error_text = 'The search server could not be reached, please try again later.'


    tvars = {
        'advanced_search': advanced_search,
#.........这里部分代码省略.........
开发者ID:MTG,项目名称:freesound,代码行数:103,代码来源:views.py


注:本文中的utils.search.solr.SolrQuery.set_group_field方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。