Ejemplo n.º 1
0
def get_mlt_query(
    si: ExtraSolrInterface,
    cd: CleanData,
    facet: bool,
    seed_pks: List[str],
    filter_query: str,
) -> SolrResponse:
    """
    By default Solr MoreLikeThis queries do not support highlighting. Thus, we
    use a special search interface and build the Solr query manually.

    :param si: SolrInterface
    :param cd: Cleaned search form data
    :param facet: Set to True to enable facets
    :param seed_pks: List of IDs of the documents for that related documents
    should be returned
    :param filter_query:
    :return: Executed SolrSearch
    """
    hl_fields = list(SOLR_OPINION_HL_FIELDS)

    # Exclude citations from MLT highlighting
    hl_fields.remove("citation")

    # Reset query for query builder
    cd["q"] = ""

    # Build main query as always
    q = build_main_query(cd, facet=facet)
    cleaned_fq = filter_query.strip()

    q.update({
        "caller": "mlt_query",
        "q": "id:(" + (" OR ".join(seed_pks)) + ")",
        "mlt": "true",  # Python boolean does not work here
        "mlt.fl": "text",
        "mlt.maxqt": settings.RELATED_MLT_MAXQT,
        "mlt.mintf": settings.RELATED_MLT_MINTF,
        "mlt.minwl": settings.RELATED_MLT_MINWL,
        "mlt.maxwl": settings.RELATED_MLT_MAXWL,
        "mlt.maxdf": settings.RELATED_MLT_MAXDF,
        # Retrieve fields as highlight replacement
        "fl": q["fl"] + "," + (",".join(hl_fields)),
        # Original query as filter query
        "fq": q["fq"] + [cleaned_fq],
        # unset fields not used for MLT
        "boost": "",
        "pf": "",
        "ps": "",
        "qf": "",
    })

    return si.mlt_query(hl_fields).add_extra(**q)