Пример #1
0
def name_only_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    This sets up various queries with boosting against the name field in the
    elasticsearch index.

    """
    d = {}

    rules = {
        'term': {'value': q, 'boost': 10},  # Exact match.
        'text': {'query': q, 'boost': 3, 'analyzer': 'standard'},
        'text': {'query': q, 'boost': 4, 'type': 'phrase'},
        'fuzzy': {'value': q, 'boost': 2, 'prefix_length': 4},
        'startswith': {'value': q, 'boost': 1.5}
    }
    for k, v in rules.iteritems():
        for field in ('name', 'app_slug', 'author'):
            d['%s__%s' % (field, k)] = v

    analyzer = _get_locale_analyzer()
    if analyzer:
        d['name_%s__text' % analyzer] = {
            'query': q, 'boost': 2.5,
            'analyzer': get_custom_analyzer(analyzer)}
    return d
Пример #2
0
def name_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    Note: This is marketplace specific. See apps/search/views.py for AMO.

    """
    more = {
        'description__match': {
            'query': q,
            'boost': 0.8,
            'type': 'phrase'
        },
    }

    analyzer = _get_locale_analyzer()
    if analyzer:
        more['description_%s__match' % analyzer] = {
            'query': q,
            'boost': 0.6,
            'type': 'phrase',
            'analyzer': get_custom_analyzer(analyzer)
        }

    more['tags__match'] = {'query': q}
    if ' ' not in q:
        more['tags__fuzzy'] = {'value': q, 'prefix_length': 1}

    return dict(more, **name_only_query(q))
Пример #3
0
def name_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    Note: This is marketplace specific. See apps/search/views.py for AMO.

    """
    more = {
        'description__text': {
            'query': q,
            'boost': 0.8,
            'type': 'phrase'
        },
    }

    analyzer = _get_locale_analyzer()
    if analyzer:
        more['description_%s__text' % analyzer] = {
            'query': q,
            'boost': 0.6,
            'type': 'phrase',
            'analyzer': analyzer
        }

    more['tags__text'] = {'query': q}

    return dict(more, **name_only_query(q))
Пример #4
0
def name_only_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    This sets up various queries with boosting against the name field in the
    elasticsearch index.

    """
    d = {}

    rules = {
        'match': {
            'query': q,
            'boost': 3,
            'analyzer': 'standard'
        },
        'match': {
            'query': q,
            'boost': 4,
            'type': 'phrase',
            'slop': 1
        },
        'startswith': {
            'value': q,
            'boost': 1.5
        }
    }

    # Only add fuzzy queries if q is a single word. It doesn't make sense to do
    # a fuzzy query for multi-word queries.
    if ' ' not in q:
        rules['fuzzy'] = {'value': q, 'boost': 2, 'prefix_length': 1}

    for k, v in rules.iteritems():
        for field in ('name', 'app_slug', 'author'):
            d['%s__%s' % (field, k)] = v

    # Exact matches need to be queried against a non-analyzed field. Let's do a
    # term query on `name_sort` for an exact match against the app name and
    # give it a good boost since this is likely what the user wants.
    d['name_sort__term'] = {'value': q, 'boost': 10}

    analyzer = _get_locale_analyzer()
    if analyzer:
        d['name_%s__match' % analyzer] = {
            'query': q,
            'boost': 2.5,
            'analyzer': get_custom_analyzer(analyzer)
        }
    return d
Пример #5
0
def name_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    Note: This is marketplace specific. See apps/search/views.py for AMO.

    """
    more = {'description__text': {'query': q, 'boost': 0.3, 'type': 'phrase'}}

    analyzer = _get_locale_analyzer()
    if analyzer:
        more['description_%s__text' % analyzer] = {'query': q,
                                                   'boost': 0.1,
                                                   'type': 'phrase',
                                                   'analyzer': analyzer}
    return dict(more, **name_only_query(q))
Пример #6
0
def name_only_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    This sets up various queries with boosting against the name field in the
    elasticsearch index.

    """
    d = {}

    rules = {
        'term': {
            'value': q,
            'boost': 10
        },  # Exact match.
        'text': {
            'query': q,
            'boost': 3,
            'analyzer': 'standard'
        },
        'text': {
            'query': q,
            'boost': 4,
            'type': 'phrase'
        },
        'fuzzy': {
            'value': q,
            'boost': 2,
            'prefix_length': 4
        },
        'startswith': {
            'value': q,
            'boost': 1.5
        }
    }
    for k, v in rules.iteritems():
        for field in ('name', 'app_slug', 'author'):
            d['%s__%s' % (field, k)] = v

    analyzer = _get_locale_analyzer()
    if analyzer:
        d['name_%s__text' % analyzer] = {
            'query': q,
            'boost': 2.5,
            'analyzer': analyzer
        }
    return d
Пример #7
0
def name_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    Note: This is marketplace specific. See apps/search/views.py for AMO.

    """
    more = {
        'description__match': {'query': q, 'boost': 0.8, 'type': 'phrase'},
    }

    analyzer = _get_locale_analyzer()
    if analyzer:
        more['description_%s__match' % analyzer] = {
            'query': q, 'boost': 0.6, 'type': 'phrase',
            'analyzer': get_custom_analyzer(analyzer)}

    more['tags__match'] = {'query': q}
    if ' ' not in q:
        more['tags__fuzzy'] = {'value': q, 'prefix_length': 1}

    return dict(more, **name_only_query(q))
Пример #8
0
def name_only_query(q):
    """
    Returns a dictionary with field/value mappings to pass to elasticsearch.

    This sets up various queries with boosting against the name field in the
    elasticsearch index.

    """
    d = {}

    rules = {
        'match': {'query': q, 'boost': 3, 'analyzer': 'standard'},
        'match': {'query': q, 'boost': 4, 'type': 'phrase'},
        'startswith': {'value': q, 'boost': 1.5}
    }

    # Only add fuzzy queries if q is a single word. It doesn't make sense to do
    # a fuzzy query for multi-word queries.
    if ' ' not in q:
        rules['fuzzy'] = {'value': q, 'boost': 2, 'prefix_length': 1}

    for k, v in rules.iteritems():
        for field in ('name', 'app_slug', 'author'):
            d['%s__%s' % (field, k)] = v

    # Exact matches need to be queried against a non-analyzed field. Let's do a
    # term query on `name_sort` for an exact match against the app name and
    # give it a good boost since this is likely what the user wants.
    d['name_sort__term'] = {'value': q, 'boost': 10}

    analyzer = _get_locale_analyzer()
    if analyzer:
        d['name_%s__match' % analyzer] = {
            'query': q, 'boost': 2.5,
            'analyzer': get_custom_analyzer(analyzer)}
    return d