Exemplo n.º 1
0
def query_string(search_phrase):
    res = es.search(index='online_library',
                    body={
                        'from': 0,
                        'size': 4,
                        'query': {
                            'query_string': {
                                'query': '*{}*'.format(search_phrase),
                                'fields': ['title^5', 'persons^5', 'genre']
                            }
                        }
                    })

    return res
Exemplo n.º 2
0
def wildcard_es(search_phrase):
    result = []
    fields = get_fields_by_boost()
    es_res = es.search(index='online_library',
                       body={
                           'from': 0,
                           'size': 4,
                           'query': {
                               'wildcard': {
                                   'title': {
                                       'value': 'my*s',
                                       'boost': 1.0,
                                       'rewrite': 'constant_score'
                                   }
                               }
                           }
                       })
    return es_res
Exemplo n.º 3
0
def search_phrase(data):
    result = []
    fields = get_fields_by_boost()

    es_res = es.search(index='online_library',
                       body={
                           'from': data.get('from'),
                           'size': data.get('size'),
                           'query': {
                               'query_string': {
                                   'query':
                                   '*{}*'.format(data.get('search_phrase')),
                                   'fields':
                                   fields,
                                   'rewrite':
                                   'constant_score'
                               }
                           }
                       })
    #
    # es_res = es.search(index='online_library', body={'from': 0, 'size': 4, 'query': {
    #     'multi_match': {'query': search_phrase, 'type': 'most_fields', 'fields': fields, 'fuzziness': 'AUTO'}}})

    hits = es_res.get('hits', None)
    count = 0
    if hits is not None:
        count = hits.get('total')

        print('count ==> {}'.format(count))
        rs = hits.get('hits')
        print('hits ==> {} ,\n\n\n\n\n\n len_hits = {}\n\n\n\n\n\n'.format(
            rs, len(rs)))

        if len(rs) > 0:
            for item in rs:
                result.append(item.get('_id'))
    return result
Exemplo n.º 4
0
def search_by_book_id(book_id):
    body = {'query': {'match': {'book_id': book_id}}}
    res = es.search(index='online_library', body=body)
    return res