Example #1
0
def getGraduacionFacet(rango):
    inferior = rango[0]
    superior = rango[1] - inferior
    return sorting.RangeFacet("graduacion",
                              0,
                              100, [inferior, superior],
                              hardend=True)
Example #2
0
def test_range_gaps():
    schema = fields.Schema(id=fields.STORED, num=fields.NUMERIC)
    ix = RamStorage().create_index(schema)
    w = ix.writer()
    for i in range(10):
        w.add_document(id=i, num=i)
    w.commit()

    with ix.searcher() as s:
        rf = sorting.RangeFacet("num", 0, 1000, [1, 2, 3])
        r = s.search(query.Every(), groupedby={"num": rf})
        assert r.groups("num") == {(0, 1): [0],
                                   (1, 3): [1, 2],
                                   (3, 6): [3, 4, 5],
                                   (6, 9): [6, 7, 8],
                                   (9, 12): [9]}
Example #3
0
def test_range_facet():
    schema = fields.Schema(id=fields.STORED, price=fields.NUMERIC)
    ix = RamStorage().create_index(schema)
    w = ix.writer()
    w.add_document(id=0, price=200)
    w.add_document(id=1, price=100)
    w.add_document(id=2)
    w.add_document(id=3, price=50)
    w.add_document(id=4, price=500)
    w.add_document(id=5, price=125)
    w.commit()

    with ix.searcher() as s:
        rf = sorting.RangeFacet("price", 0, 1000, 100)
        r = s.search(query.Every(), groupedby={"price": rf})
        assert r.groups("price") == {(0, 100): [3], (100, 200): [1, 5],
                                     (200, 300): [0], (500, 600): [4],
                                     None: [2]}
Example #4
0
def getPrecioFacet(rango):
    inferior = rango[0]
    superior = rango[1] - inferior
    return sorting.RangeFacet(
        "precioGroup", 0, 10000,
        [inferior, superior])  # 0-10€, 10-50€, 50-100€ y ya de 100 en 100.
Example #5
0
    ix = open_dir(index_path)
else:
    print ('No index found at given path')

parser = QueryParser("directions", ix.schema)
# parser = QueryParser("directions", ix.schema)
with ix.searcher() as searcher:
    # print (list(searcher.lexicon('directions')))
    while True:
        print ('Enter query string:', )
        query_str = input()
        parsed_query = parser.parse(query_str)
        print (parsed_query)
        # results = searcher.search(parsed_query, limit = 10)

        caloriesFacet = sorting.RangeFacet("calories", 0, 800, 200)
        categoriesFacet = sorting.FieldFacet("categories", allow_overlap = True)
        # mf = sorting.MultiFacet([caloriesFacet, categoriesFacet])
        # results = searcher.search(parsed_query, groupedby = mf)
        results_cats = searcher.search(parsed_query, groupedby = categoriesFacet)
        results_cals = searcher.search(parsed_query, groupedby = caloriesFacet)
        print (len(results))
        # print (results)
        
        groups_cats = results_cats.groups()
        groups_cals = results_cals.groups()
        # print (groups.keys())
        
        total_num_docs = 0
        for key in groups_cals.keys():
            # print (key)
Example #6
0
def search(query):
    query_str = query['text'][0]
    query_str = ' '.join(query_str.strip().split(','))
    parser = CustomQueryParser(ix.schema)
    with ix.searcher() as searcher:
        print(query_str)
        parsed_query = parser.parse(query_str)
        print(parsed_query)

        results = searcher.search(parsed_query)
        scores = {}

        caloriesFacet = sorting.RangeFacet("calories", 0, 800, 200)
        categoriesFacet = sorting.FieldFacet("categories", allow_overlap=True)
        # allFacets = sorting.MultiFacet([caloriesFacet, categoriesFacet])
        categories_results = searcher.search(parsed_query,
                                             groupedby=categoriesFacet)
        calories_results = searcher.search(parsed_query,
                                           groupedby=caloriesFacet)

        categories_groups = categories_results.groups()
        calories_groups = calories_results.groups()

        calories_facets, categories_facets, flag = parse_facets(query)
        print(calories_facets, categories_facets, flag)
        print(len(results), len(calories_results), len(categories_results))

        all_res = []

        if flag != -1:
            for idx in range(len(categories_results)):
                docnum = categories_results.docnum(idx)
                score = categories_results.score(idx)
                scores[docnum] = score
                # print (idx, score)
            for idx in range(len(calories_results)):
                docnum = calories_results.docnum(idx)
                score = calories_results.score(idx)
                scores[docnum] = score
                # print (idx, score)

            res_facets = -1
            doc_set = set()
            cnt = 0
            if categories_facets[0]:
                for cat in categories_facets:
                    doclist = categories_groups[cat]
                    if cnt == 0:
                        for docnum in doclist:
                            doc_set.add(docnum)
                    else:
                        temp_set = set()
                        for docnum in doclist:
                            temp_set.add(docnum)
                        doc_set = doc_set.intersection(temp_set)
                    cnt += 1
                    # print ('{} Doclist: {}'.format(cat, doclist))

                categories_doclist = list(doc_set)
                print('Categories doclist length:', len(categories_doclist))

            if calories_facets:
                doclist = calories_groups[calories_facets]
                temp_set = set()
                for docnum in doclist:
                    temp_set.add(docnum)
                if categories_facets[0]:
                    doc_set = doc_set.intersection(temp_set)
                else:
                    doc_set = temp_set

            doclist = list(doc_set)
            print('Final doclist length:', len(doclist))
            print(doclist)

            doclist.sort(key=lambda x: scores[x], reverse=True)
            print('Final doclist length:', len(doclist))
            print(doclist)

            if len(doclist) > 0:
                print(doclist)
                print(searcher.stored_fields(doclist[0]))
            else:
                print('No results found')

            for docnum in doclist:
                result = searcher.stored_fields(docnum)
                dict_res = {}
                for key in result.keys():
                    dict_res[key] = result[key]
                all_res.append(dict_res)

        else:
            if len(results) == 0:
                print('No results found')

            for result in results:
                dict_res = {}
                for key in result.keys():
                    dict_res[key] = result[key]
                all_res.append(dict_res)

        if len(all_res) > 5:
            all_res = all_res[:5]

        # for res in all_res:
        #     print (res)

    return all_res