def filter_query(q, filters, skip=None): """ Apply a list of filters to the given query. """ or_filters = defaultdict(list) for field, value in filters: if field == skip: continue if field in OR_FIELDS: or_filters[field].append(value) else: q = add_filter(q, {'term': {field: value}}) for field, value in or_filters.items(): q = add_filter(q, {'terms': {field: value}}) return q
def tabular_query(document_id, sheet, args): scored = False q = { 'match_all': {} } text = args.get('q', '').strip() if len(text): scored = True text_latin = latinize_text(text) q = { "bool": { "should": { "match": { "text": { "query": text, "cutoff_frequency": 0.0007, "operator": "and" } } }, "should": { "match": { "text_latin": { "query": text_latin, "cutoff_frequency": 0.0007, "operator": "and" } } } } } try: rows = [int(r) for r in args.getlist('row')] except Exception: rows = [] if len(rows): scored = True q = { "bool": { "must": q, "should": { "constant_score": { "filter": {'terms': {'row_id': rows}}, "boost": 1000 } } } } q = add_filter(q, {'term': {'document_id': document_id}}) q = add_filter(q, {'term': {'sheet': sheet}}) # from pprint import pprint # pprint(q) sort = [{'row_id': 'asc'}] if scored: sort.insert(0, '_score') return { 'from': 0, 'size': 100, 'query': q, 'sort': sort, '_source': ['document_id', 'sheet', 'row_id', 'raw'] }