Ejemplo n.º 1
0
def params_to_query(params: dict):
    s = Search(index=settings.ELASTICSEARCH_INDEX)
    options = {}
    errors = []
    if 'searchterm' in params and params['searchterm'] is not "":
        s = s.query('query_string', query=params['searchterm'])
        options['searchterm'] = params['searchterm']

    try:
        lat = float(params.get('lat', ''))
        lng = float(params.get('lng', ''))
        radius = int(params.get('radius', ''))
        s = s.filter("geo_distance", distance=str(radius) + "m", coordinates={
            "lat": lat,
            "lon": lng,
        })
        options['lat'] = str(lat)
        options['lng'] = str(lng)
        options['radius'] = str(radius)
        options['location_formatted'] = latlng_to_address(lat, lng)
    except ValueError:
        pass
    if 'document-type' in params:
        split = params['document-type'].split(",")
        s = s.filter('terms', _type=[i + "_document" for i in split])
        options["document_type"] = split
    if 'after' in params:
        s = add_date(s, params['after'], "gte", options, errors)
    if 'before' in params:
        s = add_date(s, params['before'], "lte", options, errors)

    return options, s, errors
Ejemplo n.º 2
0
    def search(self):
        s = super().search()
        try:
            lat = float(self.params.get('lat', ''))
            lng = float(self.params.get('lng', ''))
            radius = int(self.params.get('radius', ''))
            s = s.filter("geo_distance", distance=str(radius) + "m", coordinates={
                "lat": lat,
                "lon": lng,
            })
            self.options['lat'] = str(lat)
            self.options['lng'] = str(lng)
            self.options['radius'] = str(radius)
            self.options['location_formatted'] = latlng_to_address(lat, lng)
        except ValueError:
            pass

        if 'after' in self.params:
            # options['after'] added by _add_date_after
            s = _add_date_after(s, self.params, self.options, self.errors)
        if 'before' in self.params:
            # options['before'] added by _add_date_before
            s = _add_date_before(s, self.params, self.options, self.errors)

        return s
def search_format_geo(_, lat, lng):
    return JsonResponse(
        {
            "lat": lat,
            "lng": lng,
            "formatted": latlng_to_address(lat, lng)
        },
        safe=False)
    def search(self) -> Search:
        search = super().search()  # type: Search
        try:
            lat = float(self.params.get("lat", ""))
            lng = float(self.params.get("lng", ""))
            radius = int(self.params.get("radius", ""))
            search = search.filter(
                "geo_distance",
                distance=str(radius) + "m",
                coordinates={
                    "lat": lat,
                    "lon": lng
                },
            )
            self.options["lat"] = str(lat)
            self.options["lng"] = str(lng)
            self.options["radius"] = str(radius)
            self.options["location_formatted"] = latlng_to_address(lat, lng)
        except ValueError:
            pass

        if "after" in self.params:
            # options['after'] added by _add_date_after
            search = _add_date_after(search, self.params, self.options,
                                     self.errors)
        if "before" in self.params:
            # options['before'] added by _add_date_before
            search = _add_date_before(search, self.params, self.options,
                                      self.errors)

        # indices_boost: Titles often repeat the organization name and the test contains person names, but
        # when searching for those proper nouns, the person/organization itself should be at the top
        # _source: Take only the fields we use, and especially ignore the huge parsed_text
        search.update_from_dict({
            "indices_boost": [
                {
                    DOCUMENT_INDICES["person"]: 4
                },
                {
                    DOCUMENT_INDICES["organization"]: 4
                },
                {
                    DOCUMENT_INDICES["paper"]: 2
                },
            ],
            "_source": ["id", "name", "legal_date", "reference_number"],
        })

        # N.B.: indexing reset from and size
        if self.limit:
            if self.offset:
                search = search[self.offset:self.limit + self.offset]
            else:
                search = search[:self.limit]

        return search
Ejemplo n.º 5
0
def params_to_human_string(params: dict):
    from mainapp.documents import DOCUMENT_TYPE_NAMES_PL

    if 'document-type' in params:
        split = params['document-type'].split(",")
        what = []
        for el in split:
            what.append(DOCUMENT_TYPE_NAMES_PL[el])
        if len(what) > 1:
            last_el = what.pop()
            description = ", ".join(what)
            description += " " + pgettext('Search query', 'and') + " " + last_el
        else:
            description = what[0]

    else:
        description = pgettext('Search query', 'Documents')

    strs = []

    if 'searchterm' in params and params['searchterm'] != '':
        strs.append(pgettext('Search query', 'containing "%STR%"').replace('%STR%', params['searchterm']))

    if 'person' in params:
        person = Person.objects.get(pk=params['person'])
        if person:
            strs.append(pgettext('Search query', 'mentioning %FROM%').replace('%FROM%', person.__str__()))

    if 'organization' in params:
        organization = Organization.objects.get(pk=params['organization'])
        if organization:
            strs.append(pgettext('Search query', 'assigned to %TO%').replace('%TO%', organization.__str__()))

    if 'radius' in params:
        place_name = latlng_to_address(params['lat'], params['lng'])
        locstr = pgettext('Search query', 'with a location within %DISTANCE%m around "%PLACE%"')
        strs.append(locstr.replace('%DISTANCE%', params['radius']).replace('%PLACE%', place_name))

    if 'before' in params and 'after' in params:
        strs.append(
            pgettext('Search query', 'published from %FROM% to %TO%')
                .replace('%FROM%', params['after'])
                .replace('%TO%', params['before']))
    elif 'before' in params:
        strs.append(pgettext('Search query', 'published before %TO%').replace('%TO%', params['before']))
    elif 'after' in params:
        strs.append(pgettext('Search query', 'published after %FROM%').replace('%FROM%', params['after']))

    if len(strs) > 0:
        description += " " + ", ".join(strs)

    return description
Ejemplo n.º 6
0
def params_to_human_string(params: dict):
    if "document-type" in params:
        split = params["document-type"].split(",")
        what = []
        for el in split:
            what.append(DOCUMENT_TYPE_NAMES_PL[el])
        if len(what) > 1:
            last_el = what.pop()
            description = ", ".join(what)
            description += " " + pgettext("Search query",
                                          "and") + " " + last_el
        else:
            description = what[0]

    else:
        description = pgettext("Search query", "Documents")

    strs = []

    if "searchterm" in params and params["searchterm"] != "":
        strs.append(
            pgettext("Search query",
                     'containing "%STR%"').replace("%STR%",
                                                   params["searchterm"]))

    if "person" in params:
        person = Person.objects.get(pk=params["person"])
        if person:
            strs.append(
                pgettext("Search query",
                         "mentioning %FROM%").replace("%FROM%", str(person)))

    if "organization" in params:
        organization = Organization.objects.get(pk=params["organization"])
        if organization:
            strs.append(
                pgettext("Search query",
                         "assigned to %TO%").replace("%TO%",
                                                     str(organization)))

    if "radius" in params:
        place_name = latlng_to_address(params["lat"], params["lng"])
        locstr = pgettext(
            "Search query",
            'with a location within %DISTANCE%m around "%PLACE%"')
        strs.append(
            locstr.replace("%DISTANCE%",
                           params["radius"]).replace("%PLACE%", place_name))

    if "before" in params and "after" in params:
        strs.append(
            pgettext("Search query", "published from %FROM% to %TO%").replace(
                "%FROM%", params["after"]).replace("%TO%", params["before"]))
    elif "before" in params:
        strs.append(
            pgettext("Search query",
                     "published before %TO%").replace("%TO%",
                                                      params["before"]))
    elif "after" in params:
        strs.append(
            pgettext("Search query",
                     "published after %FROM%").replace("%FROM%",
                                                       params["after"]))

    if len(strs) > 0:
        description += " " + ", ".join(strs)

    return description