Beispiel #1
0
def search(userquery: str):
    results = []

    # TODO: figure out how to escape dashes
    # "D-Feet" seems to be interpreted as "d and not feet"
    userquery = userquery.replace("-", " ")

    # TODO: should input be sanitized here?
    name_query = redisearch.Query(f"@name:'{userquery}'").no_content()

    # redisearch does not support fuzzy search for strings with whitespace
    if contains_whitespace(userquery):
        generic_query = redisearch.Query(userquery).no_content()
    else:
        generic_query = redisearch.Query(f"%{userquery}%").no_content()

    search_results = redis_search.search(name_query)
    for doc in search_results.docs:
        results.append(doc.id)

    search_results = redis_search.search(generic_query)
    for doc in search_results.docs:
        results.append(doc.id)

    results = list(dict.fromkeys(results))
    if not len(results):
        return []

    appids = tuple(doc_id.replace("fts", "apps") for doc_id in results)
    ret = list_apps_summary(appids=appids, sort=False)

    return ret
Beispiel #2
0
 def get_strings(self, size: int, feature: str) -> List[str]:
     """Get strings corresponding to feature size and query feature."""
     query = (
         redisearch.Query(feature).verbatim().limit_fields('ng').add_filter(
             redisearch.NumericFilter('sz', size,
                                      size)).return_fields('term'))
     return [document.term for document in self._db.get(query).docs]
Beispiel #3
0
def filter_fromdb(task_id,
                  search_filter,
                  start,
                  length=-1,
                  order_col="row",
                  order_asc=True):
    '''
    task_id,
    search_filter,
    start,length = -1,
    order_col="row",
    order_asc=True
    '''
    result = {}
    client = redisearch.Client(task_id)
    result['recordsTotal'] = int(client.info()['num_docs'])

    #manual = False # manually made because redisearch sucks
    if length == -1:
        length = result['recordsTotal'] - start

    # if there is filter or length == -1 we return everything
    # hay que devolver todo porque necesitamos contar el número de filas
    if search_filter:
        query = redisearch.Query("*").sort_by(order_col, order_asc).paging(
            0, result['recordsTotal'])
        documents = client.search(query).docs
        filtered_docs = list(
            filter(lambda doc: dofilter(search_filter, doc), documents))
        result['recordsFiltered'] = len(filtered_docs)
        result['data'] = filtered_docs[start:start + length]
    else:
        query = redisearch.Query("*").sort_by(order_col,
                                              order_asc).paging(start, length)
        res = client.search(query)
        result['recordsFiltered'] = res.total
        result['data'] = res.docs

    #if searchtype == "exclude":
    #    #searchtext = searchquery.replace(">","\\>") -- @col:query
    #    querystr = "-(@%s:\"%s\")" % (colname,searchquery)
    #else: #searchtype == "exact"
    #    querystr = "@%s:\"%s\"" % (colname,searchquery)
    return result
Beispiel #4
0
def search(userquery: str):
    results = []

    # TODO: figure out how to escape dashes
    # "D-Feet" seems to be interpreted as "d and not feet"
    userquery = userquery.replace("-", " ")

    # This seems to confuse redisearch too
    userquery = userquery.replace(".*", "*")

    # Remove reserved characters
    reserved_chars = [
        "@", "!", "{", "}", "(", ")", "|", "-", "=", ">", "[", "]", ":", ";"
    ]
    for char in reserved_chars:
        userquery = userquery.replace(char, "")

    # TODO: should input be sanitized here?
    name_query = redisearch.Query(f"@name:'{userquery}'").no_content()

    # redisearch does not support fuzzy search for non-alphabet strings
    if userquery.isalpha():
        generic_query = redisearch.Query(f"%{userquery}%").no_content()
    else:
        generic_query = redisearch.Query(userquery).no_content()

    # TODO: Backend API doesn't support paging so bring fifty results
    # instead of just 10, which is the redisearch default
    name_query.paging(0, 50)
    generic_query.paging(0, 50)

    search_results = redis_search.search(name_query)
    for doc in search_results.docs:
        results.append(doc.id)

    search_results = redis_search.search(generic_query)
    for doc in search_results.docs:
        results.append(doc.id)

    if not len(results):
        return None

    return list(dict.fromkeys(results))
Beispiel #5
0
def do_search(chat_id, query, start=0, size=10):
    q = redisearch.Query(query)
    q.sort_by('ts', asc=False)
    q.paging(start, size)
    # q.summarize(fields=['msg'], context_len=24)
    q.language('chinese')

    cli = get_redisearch_cli(chat_id)
    res = cli.search(q)
    return res
Beispiel #6
0
def search(userquery: str):
    # TODO: figure out how to escape dashes
    # "D-Feet" seems to be interpreted as "d and not feet"
    userquery = userquery.replace("-", " ")

    results = []

    # TODO: should input be sanitized here?
    name_query = redisearch.Query(f"@name:'{userquery}'").no_content()
    generic_query = redisearch.Query(userquery).no_content()

    search_results = redis_search.search(name_query)
    for doc in search_results.docs:
        results.append(doc.id)

    search_results = redis_search.search(generic_query)
    for doc in search_results.docs:
        results.append(doc.id)

    results = list(dict.fromkeys(results))
    appids = tuple(doc_id.replace("fts", "apps") for doc_id in results)

    ret = list_apps_summary(appids=appids, sort=False)
    return ret
Beispiel #7
0
def search(
    index: str,
    query_str: str,
    only_id: Optional[bool] = None,
    verbatim: Optional[bool] = None,
    offset: Optional[int] = None,
    limit: Optional[int] = None,
    sort_by: Optional[str] = None,
    sort_asc: Optional[bool] = None,
) -> redisearch.Result:
    """Do a search on the redisearch indexes
    Args:
        index: The index to search
        query_str: Redisearch search query syntax: https://oss.redislabs.com/redisearch/Query_Syntax.html
        only_id: whether or not to only fetch document ids, and not their contents (default true)
        verbatim: whether or not to use stemming for query expansion in the query_str
        offset: the offset to start the query from, this can be used for pagination (defaults to 0, aka the start of the query)
        limit: the number of results to fetch from the query (can be set to 0 to simply get a count of the query results)
        sort_by: the sortable field name to sort by for this query
        sort_asc: (Only relevant if sort_by is set), sort the results in ascending order if true, descending if false
    Returns:
        redisearch result object
    """
    # Set some sensible defaults
    only_id = True if only_id is None else only_id
    verbatim = False if verbatim is None else verbatim
    offset = 0 if offset is None else offset
    limit = 10 if limit is None else limit
    sort_by = "" if sort_by is None else sort_by
    sort_asc = True if sort_asc is None else sort_asc

    client = _get_redisearch_index_client(index)
    query = redisearch.Query(query_str).paging(offset, limit)
    if only_id:
        query.no_content()
    if verbatim:
        query.verbatim()
    if sort_by:
        query.sort_by(sort_by, sort_asc)
    return client.search(query)