def get_object_list(self, request):
        offset = long(request.GET.get("offset", 0))
        limit = long(request.GET.get("limit", self._meta.limit))

        sort = self.get_sorting(request)

        q = request.GET.get("q")

        if q:
            query = pyes.StringQuery(q)
        else:
            query = pyes.MatchAllQuery()

        size = (limit + offset) - (1 if offset else 0)
        start = offset + (2 if offset >= limit else 1)

        print "offset ", offset, limit.__class__
        print "limit ", limit, limit.__class__
        print "start ", start, start.__class__
        print "size ", size, size.__class__
        print "sort", sort

        # refresh the index before query
        self.es.refresh(self._meta.indices[0])

        search = pyes.query.Search(query=query,
                                   start=start,
                                   size=size,
                                   sort=sort)

        results = self.es.search(search, indices=self._meta.indices)
        return results
Exemple #2
0
def search():
    es = pyes.ES('127.0.0.1:9200')
    page_no = request.GET.get('page_no')
    query_string = request.GET.get('query')
    query = pyes.StringQuery(query_string)
    result = es.search(query=query, search_fields="_all")
    bill_list = []
    session = models.DBSession()
    for res in result['hits']['hits']:
        id = res['_source']['id']
        bill = session.query(models.BillRevision).get(id)
        if not bill:
            log.info('bill_revs record with %r not found.' % id)
            continue

        bill_list.append(bill)
    if bill_list:
        pages = utils.Pagination(settings.ITEM_PER_PAGE,
                                 settings.PAGE_DISPLAYED, len(bill_list),
                                 page_no)
    else:
        pages = None
    if pages:
        bill_list = bill_list[pages.first:pages.last]

    return {'bill': bill_list, 'query': query_string, 'pages': pages}
Exemple #3
0
def archival_storage_list_display(request, current_page_number=None):
    form = forms.StorageSearchForm()

    total_size = 0

    # get ElasticSearch stats
    aip_indexed_file_count = advanced_search.indexed_count('aips')

    # get AIPs
    conn = elasticSearchFunctions.connect_and_create_index('aips')
    aipResults = conn.search(pyes.StringQuery('*'), doc_types=['aip'])
    aips = []

    #if aipResults._total != None:
    if len(aipResults) > 0:
        for aip in aipResults:
            aips.append(aip)

    # handle pagination
    page = helpers.pager(aips, 10, current_page_number)

    sips = []
    for aip in page['objects']:
        sip = {}
        sip['href'] = aip.filePath.replace(AIPSTOREPATH + '/', "AIPsStore/")
        sip['name'] = aip.name
        sip['uuid'] = aip.uuid

        #sip['date'] = str(aip.date)[0:19].replace('T', ' ')
        sip['date'] = aip.created

        try:
            size = float(aip.size)
            total_size = total_size + size
            sip['size'] = '{0:.2f} MB'.format(size)
        except:
            sip['size'] = 'Removed'

        sips.append(sip)

    order_by = request.GET.get('order_by', 'name')
    sort_by = request.GET.get('sort_by', 'up')

    def sort_aips(sip):
        value = 0
        if 'name' == order_by:
            value = sip['name'].lower()
        else:
            value = sip[order_by]
        return value

    sips = sorted(sips, key=sort_aips)

    if sort_by == 'down':
        sips.reverse()

    total_size = '{0:.2f}'.format(total_size)

    return render(request, 'archival_storage/archival_storage.html', locals())
Exemple #4
0
def query():
    # 查询nick中包含压力的记录
    q = pyes.StringQuery(u"压力", 'nick')
    results = conn.search(q)

    for r in results:
        print u"查询nick中包含压力的记录", r['nick'], r['city']

    # 查询city中包含成都的数据
    q = pyes.StringQuery(u"成都", 'city')
    results = conn.search(q)

    for r in results:
        print u"查询city中包含成都的数据", r['nick'], r['city']

    # 查询nick中包含很小或没有的数据
    q = pyes.StringQuery(u"很小 OR 没有", 'nick')
    results = conn.search(q)

    for r in results:
        print u"查询nick中包含很小或没有的数据", r['nick'], r['city']
Exemple #5
0
def preservation_planning_fpr_search(request, current_page_number = None):
    if current_page_number == None:                
        current_page_number = 1

    query = request.GET.get('query', '')

    if query == '':
        # No query in the URL parameters list, try to see if we've got an existing query going from a previous page...
        query = request.session['fpr_query']
  
        # No query from a previous page either
        if query == '':
            query = '*'
            return HttpResponse('No query.')


    request.session['fpr_query'] = query # Save this for pagination...
    conn = pyes.ES(elasticSearchFunctions.getElasticsearchServerHostAndPort())

    indexes = conn.get_indices()

    if 'fpr_file' not in indexes:
        # Grab relevant FPR data from the DB
        results = get_fpr_table()
        request.session['fpr_results'] = results

        # Setup indexing for some Elastic Search action.
        for row in results:
            conn.index(row, 'fpr_file', 'fpr_files')
    else:
        results = request.session['fpr_results']
    
    # do fulltext search
    q = pyes.StringQuery(query)
    s = pyes.Search(q)

    try:
        results = conn.search_raw(s, size=len(results), indices='fpr_file')
    except:
        return HttpResponse('Error accessing index.')
    
    form = FPRSearchForm()

    search_hits = []

    for row in results.hits.hits:
        search_hits.append(row['_source'].copy())

    page = helpers.pager(search_hits, results_per_page, current_page_number)
    hit_count = len(search_hits) 
  
    return render(request, 'main/preservation_planning_fpr.html', locals())
Exemple #6
0
def search():
    es = pyes.ES('127.0.0.1:9200') 
    query_string = request.GET.get('query')
    query = pyes.StringQuery(query_string)
    result = es.search(query=query)
    bill = []
    for res in result['hits']['hits']:
        source = res['_source']
        temp = {}
        for k in source.keys():
            if k != 'document':
                temp[k] = source[k]
        bill.append(temp)
    print bill
    return dict(bill=bill)
Exemple #7
0
def get_search_results(q, extra_filter=None, sort='trefwoord'):
    es = pyes.ES(settings.ELASTIC_SEARCH)
    if q:
        q1 = pyes.StringQuery(q)
    else:
        q1 = pyes.MatchAllQuery()

    if extra_filter:
        q2 = q1.search(filter=extra_filter)
    else:
        q2 = q1.search()
    q2.facet.add_term_facet('taal')
    q2.facet.add_term_facet('woordsoort')
    q2.facet.add_term_facet('sfeer', size="60")

    results = es.search(query=q2,
                        indices='nedind',
                        doc_types=['trefwoord'],
                        sort=sort)
    return results
Exemple #8
0
def query_clause(index, queries, ops, fields, types):
    if fields[index] == '':
        search_fields = []
    else:
        search_fields = [fields[index]]

    if (types[index] == 'term'):
        # a blank term should be ignored because it prevents any results: you
        # can never find a blank term
        #
        # TODO: add condition to deal with a query with no clauses because all have
        #       been ignored
        if (queries[index] == ''):
            return
        else:
            if (fields[index] != ''):
                 term_field = fields[index]
            else:
                term_field = '_all'
            return pyes.TermQuery(term_field, queries[index])
    else:
        return pyes.StringQuery(queries[index], search_fields=search_fields)
Exemple #9
0
def query_submissions(q='',
                      fq=None,
                      sort='score desc',
                      start=0,
                      docs=10,
                      date=None,
                      facets=None):
    (sort_field, sort_order) = sort.split(' ')
    if sort_field == 'score':
        sort_field = '_score'
    sort = {sort_field: {'order': sort_order}}
    query = pyes.query.BoolQuery()
    query.add_must(pyes.StringQuery(q, default_operator="AND"))
    rest = True
    x = 0
    result = []
    while rest:
        y = fq.find(":", x)
        if y == -1:
            break
        temp = fq[x:y]
        x = y + 1
        if fq[x:x + 5] == """:
            y = fq.find(""", x + 5)
            if y == -1:
                break
            result.append((temp, fq[x + 5:y]))
            x = y + 6
            if x > len(fq):
                break
        else:
            y = fq.find(";", x)
            if y == -1:
                result.append((temp, fq[x:len(fq)]))
                break
            else:
                result.append((temp, fq[x:y]))
                x = y + 1
    for sfq in result:
        if sfq[0] == 'date':
            (year, month) = sfq[1].split('-')
            date_start = datetime.datetime(int(year), int(month), 1)
            date_end = date_start + dateutil.relativedelta.relativedelta(
                months=+1, seconds=-1)
            query.add_must(
                pyes.RangeQuery(
                    qrange=pyes.ESRange('date', date_start, date_end)))
        else:
            query.add_must(pyes.TermQuery(field=sfq[0], value=sfq[1]))
    search = pyes.query.Search(query=query,
                               fields=[''],
                               start=start,
                               size=docs,
                               sort=sort)
    search.facet.add_term_facet('type')
    search.facet.add_term_facet('rs')
    search.facet.add_term_facet('committee')
    search.facet.add_date_facet(field='date', name='date', interval='month')
    es = pyes.ES(app.config['ES_HOST'] + ':' + str(app.config['ES_PORT']))
    es.default_indices = [app.config['ES_INDEX_NAME_PREFIX'] + '-latest']
    es.refresh()
    result = es.search(search, model=lambda x, y: y)
    ret = {
        'numhits': result.total,
        'maxscore': result.max_score,
        'result': [],
        'facets': {}
    }
    if result.max_score is not None:
        ret['maxscore'] = result.max_score
    for key in result.facets:
        ret['facets'][key] = {}
        if result.facets[key]['_type'] == 'date_histogram':
            for subval in result.facets[key]['entries']:
                ret['facets'][key][datetime.datetime.fromtimestamp(
                    int(subval['time']) /
                    1000).strftime('%Y-%m')] = subval['count']
        if result.facets[key]['_type'] == 'terms':
            for subval in result.facets[key]['terms']:
                ret['facets'][key][subval['term']] = subval['count']
    for r in result:
        ret['result'].append({'_id': str(r['_id']), 'score': r['_score']})
    return ret
Exemple #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyes

index = "test2"
doc_type = "test"

es = pyes.ES(["http://127.0.0.1:9200"])

es.create_index_if_missing(index)
for i in range(1, 100):
    es.index({"number": i}, index=index, doc_type=doc_type)

es.refresh([index])

query = pyes.StringQuery("*")
search = pyes.query.Search(query=query,
                           start=0,
                           size=10,
                           sort=[{
                               "number": "asc"
                           }],
                           fields=["number"])
results = es.search(search, indices=[index], doc_types=[doc_type])
print[i for i in results]

query2 = pyes.StringQuery("*")
search2 = pyes.query.Search(query=query2,
                            start=20,
                            size=20,
                            sort=[{