예제 #1
0
 def collector(self,
               count=None,
               sort=None,
               reverse=False,
               scores=False,
               maxscore=False):
     if count is None:
         return search.CachingCollector.create(True, float('inf'))
     count = min(count, self.maxDoc() or 1)
     if sort is None:
         return search.TopScoreDocCollector.create(count)
     if isinstance(sort, string_types):
         sort = self.sortfield(sort, reverse=reverse)
     if not isinstance(sort, search.Sort):
         sort = search.Sort(sort)
     return search.TopFieldCollector.create(sort, count, True, scores,
                                            maxscore)
예제 #2
0
def test_grouping(tempdir, indexer, zipcodes):
    field = indexer.fields['location'] = engine.NestedField(
        'state.county.city', docValuesType='sorted')
    for doc in zipcodes:
        if doc['state'] in ('CA', 'AK', 'WY', 'PR'):
            lat, lng = ('{0:08.3f}'.format(doc.pop(l))
                        for l in ['latitude', 'longitude'])
            location = '.'.join(doc[name]
                                for name in ['state', 'county', 'city'])
            indexer.add(doc, latitude=lat, longitude=lng, location=location)
    indexer.commit()
    states = list(indexer.terms('state'))
    assert states[0] == 'AK' and states[-1] == 'WY'
    counties = [
        term.split('.')[-1] for term in indexer.terms('state.county', 'CA')
    ]
    hits = indexer.search(field.prefix('CA'))
    assert sorted({hit['county'] for hit in hits}) == counties
    assert counties[0] == 'Alameda' and counties[-1] == 'Yuba'
    cities = [
        term.split('.')[-1]
        for term in indexer.terms('state.county.city', 'CA.Los Angeles')
    ]
    hits = indexer.search(field.prefix('CA.Los Angeles'))
    assert sorted({hit['city'] for hit in hits}) == cities
    assert cities[0] == 'Acton' and cities[-1] == 'Woodland Hills'
    (hit, ) = indexer.search('zipcode:90210')
    assert hit['state'] == 'CA' and hit['county'] == 'Los Angeles' and hit[
        'city'] == 'Beverly Hills' and hit['longitude'] == '-118.406'
    query = Q.prefix('zipcode', '90')
    ((field, facets), ) = indexer.facets(query, 'state.county').items()
    assert field == 'state.county'
    la, orange = sorted(filter(facets.get, facets))
    assert la == 'CA.Los Angeles' and facets[la] > 100
    assert orange == 'CA.Orange' and facets[orange] > 10
    queries = {
        term: Q.term(field, term)
        for term in indexer.terms(field, 'CA.')
    }
    ((field, facets), ) = indexer.facets(query, **{field: queries}).items()
    assert all(value.startswith('CA.')
               for value in facets) and set(facets) == set(queries)
    assert facets['CA.Los Angeles'] == 264
    groups = indexer.groupby(field, Q.term('state', 'CA'), count=1)
    assert len(groups) == 1 < groups.count
    (hits, ) = groups
    assert hits.value == 'CA.Los Angeles' and len(
        hits) == 1 and hits.count > 100
    grouping = engine.documents.GroupingSearch(field,
                                               sort=search.Sort(
                                                   indexer.sortfield(field)),
                                               cache=False,
                                               allGroups=True)
    assert all(
        grouping.search(indexer.indexSearcher, Q.alldocs()).facets.values())
    assert len(grouping) == len(list(grouping)) > 100
    assert set(grouping) > set(facets)
    hits = indexer.search(query, timeout=-1)
    assert not hits and not hits.count and math.isnan(hits.maxscore)
    hits = indexer.search(query, timeout=10)
    assert len(hits) == hits.count == indexer.count(
        query) and hits.maxscore == 1.0
    directory = store.RAMDirectory()
    query = Q.term('state', 'CA')
    size = indexer.copy(directory, query)
    searcher = engine.IndexSearcher(directory)
    assert len(searcher) == size and list(searcher.terms('state')) == ['CA']
    path = os.path.join(tempdir, 'temp')
    size = indexer.copy(path, exclude=query, merge=1)
    assert len(searcher) + size == len(indexer)
    searcher = engine.IndexSearcher(path)
    assert len(searcher.segments) == 1 and 'CA' not in searcher.terms('state')
    directory.close()