logo
down
shadow

Haystack and ElasticSearch only exact search


Haystack and ElasticSearch only exact search

Content Index :


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

partial word search with haystack/elasticsearch


Tag : python , By : yatil
Date : March 29 2020, 07:55 AM
To fix the issue you can do I think there's a bug in haystack in elasticsearch_backend.py which is not using pyelasticsearch properly line 868 looks like:
self.conn.put_mapping('modelresult', current_mapping, index=self.index_name)
self.conn.put_mapping(doc_type='modelresult', mapping=current_mapping, index=self.index_name)

How can I do a fuzzy search using django-haystack and the elasticsearch backend?


Tag : django , By : beebob
Date : March 29 2020, 07:55 AM
wish helps you No need to fork Haystack, you can update that method in your own backend (for more details, see Stretching Haystack's ElasticSearch Backend). The build_search_kwargs method returns a dictionary so you can just modify the original return value.
Disclaimer: this code is just an example of how you could update your own backend, not how to implement fuzzy search.
class FuzzyBackend(ElasticsearchSearchBackend):
    def build_search_kwargs(self, query_string, **kwargs):
        fuzzy = kwargs.pop('fuzzy', False)
        fuzzy_field = kwargs.pop('min_similarity', '')
        search_kwargs = super(FuzzyBackend, self).build_search_kwargs(
                query_string, kwargs)
        if fuzzy:
            search_kwargs = {'fuzzy': {fuzzy_field: query_string}}
        return search_kwargs

Search for multiple words elasticsearch haystack


Tag : elasticsearch , By : chudq7
Date : March 29 2020, 07:55 AM
this one helps. I fell into this issue during this month.
In order to perform the correct query you'll need override some haystack objects. I found this article very helpful Extending Haystack’s Elasticsearch backend. Quite complicated at the beginning, but once understand how it works... it works :-)
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from django.conf import settings

from haystack.backends.elasticsearch_backend import (
    ElasticsearchSearchBackend, ElasticsearchSearchEngine, ElasticsearchSearchQuery)
from haystack.query import SearchQuerySet


class ElasticsearchEngineBackendCustom(ElasticsearchSearchBackend):
    DEFAULT_ANALYZER = "snowball"

    def __init__(self, connection_alias, **connection_options):
        super(ElasticsearchEngineBackendCustom, self).__init__(connection_alias, **connection_options)

        user_settings = getattr(settings, 'ELASTICSEARCH_INDEX_SETTINGS', {})
        if user_settings:
            setattr(self, 'DEFAULT_SETTINGS', user_settings)

        user_analyzer = getattr(settings, 'ELASTICSEARCH_DEFAULT_ANALYZER', '')
        if user_analyzer:
            setattr(self, 'DEFAULT_ANALYZER', user_analyzer)

    def build_search_kwargs(self, query_string, sort_by=None, start_offset=0, end_offset=None,
                            fields='', highlight=False, facets=None,
                            date_facets=None, query_facets=None,
                            narrow_queries=None, spelling_query=None,
                            within=None, dwithin=None, distance_point=None,
                            models=None, limit_to_registered_models=None,
                            result_class=None, multi_match=None):

        out = super(ElasticsearchEngineBackendCustom, self).build_search_kwargs(query_string, sort_by, start_offset,
                                                                                end_offset,
                                                                                fields, highlight, facets,
                                                                                date_facets, query_facets,
                                                                                narrow_queries, spelling_query,
                                                                                within, dwithin, distance_point,
                                                                                models, limit_to_registered_models,
                                                                                result_class)

        if multi_match:
             out['query'] = {
                'multi_match': {
                    'query': multi_match['query'],
                    'fields': multi_match['fields'],
                    'tie_breaker': multi_match['tie_breaker'],
                    'minimum_should_match': multi_match['minimum_should_match'],
                }
            }

        return out

    def build_schema(self, fields):
        content_field_name, mapping = super(ElasticsearchEngineBackendCustom, self).build_schema(fields)

        for field_name, field_class in fields.items():
            field_mapping = mapping[field_class.index_fieldname]

            if field_mapping['type'] == 'string' and field_class.indexed:
                if not hasattr(field_class, 'facet_for') or field_class.field_type in ('ngram', 'edge_ngram'):
                    field_mapping['analyzer'] = getattr(field_class, 'analyzer', self.DEFAULT_ANALYZER)
            mapping.update({field_class.index_fieldname: field_mapping})

        return content_field_name, mapping

    def multi_match_run(self, query, fields, minimum_should_match, tie_breaker):
        from elasticsearch_dsl import Search
        from elasticsearch_dsl.query import MultiMatch

        raw = Search().using(self.conn).query(
            MultiMatch(query=u'{}'.format(query), fields=fields, minimum_should_match=minimum_should_match, tie_breaker=tie_breaker)
        ).execute()

        return self._process_results(raw)


class ElasticsearchSearchQueryCustom(ElasticsearchSearchQuery):
    def multi_match(self, query, fields, minimum_should_match, tie_breaker):
        results = self.backend.multi_match_run(query, fields, minimum_should_match, tie_breaker)
        self._results = results.get('results', [])
        self._hit_count = results.get('hits', 0)

    def add_multi_match_query(self, query, fields, minimum_should_match, tie_breaker):
        self.multi_match_query = {
            'query': query,
            'fields': fields,
            'minimum_should_match': minimum_should_match,
            'tie_breaker': tie_breaker
        }

    def build_params(self, spelling_query=None, **kwargs):
        search_kwargs = super(ElasticsearchSearchQueryCustom, self).build_params(spelling_query, **kwargs)
        if self.multi_match_query:
            search_kwargs['multi_match'] = self.multi_match_query

        return search_kwargs


class ElasticsearchSearchQuerySetCustom(SearchQuerySet):
    def multi_match(self, query, fields, minimum_should_match="35%", tie_breaker=0.3):
        clone = self._clone()
        clone.query.add_multi_match_query(query, fields, minimum_should_match, tie_breaker)
        clone.query.multi_match(query, fields, minimum_should_match, tie_breaker)
        return clone


class ElasticsearchEngineCustom(ElasticsearchSearchEngine):
    backend = ElasticsearchEngineBackendCustom
    query = ElasticsearchSearchQueryCustom
ELASTICSEARCH_DEFAULT_ANALYZER = 'italian'
ELASTICSEARCH_INDEX_SETTINGS = {
    "settings": {[...]}
}
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from haystack.forms import SearchForm

from .backend import ElasticsearchSearchQuerySetCustom


class SearchFormCustom(SearchForm):
    def search(self):
        query = self.searchqueryset.query.clean(self.cleaned_data.get('q'))
        if not self.is_valid() or not query:
            return self.no_query_found()

        sqs = ElasticsearchSearchQuerySetCustom().multi_match(query, ['title^8', 'text^0.5'])

        return sqs
urlpatterns = patterns(
    'search.views',
    url('^$', search_view_factory(form_class=SearchFormCustom), name='haystack-search'),
)

Using django-haystack +Elasticsearch how can I search subsets of a word?


Tag : python , By : smbrant
Date : March 29 2020, 07:55 AM
I wish did fix the issue. If you want results like "xyzapplexyz", then you would need to use ngram analyzer instead of EdgeNGram or you could use both depending on your requirements. EdgeNGram generates tokens only from the beginning.
with NGram apple will be one of the generated tokens for term xyzapplexyz assuming max_gram >=5 and you will get expected results, also search_analyzer needs to be different or you will get weird results.

Highlight exact phrase with haystack/elasticsearch in Django


Tag : python , By : algoRhythm99
Date : March 29 2020, 07:55 AM
it fixes the issue You can build your own highlighter class as documentation states if default highlighter implementation doesn't work for you.
Related Posts Related QUESTIONS :
  • match_phrase_prefix query not working with nested aggregation
  • what is the least version of kibana that supports painless language
  • How to grok a pipe-delimited string in a log line
  • how to convert elastic query aggregate filter into nest query in .Net core
  • Really huge query or optimizing an elasticsearch update
  • Getting error on Elastic Search query [ImageURL] query malformed, no start_object after query name
  • How do i disqualify a parent document based on a key value pair in a child doc in elasticsearch
  • Query documents that contains all values in nested array Elasticsearch
  • How I can visualize elasticsearch metrics in prometheus?, both installed in a gke cluster
  • How to periodically update ElasticSearch index? UPDATE or REBUILD?
  • Tweaking relevance score of only certain queries
  • Nested boolean aggregation in elastic
  • Some Elastic fields DSL query searchable and some not
  • Elastic Search - search token aliasing
  • Elasticsearch : Search with special character Open & Close parentheses
  • Non indexed field in sort clause
  • Elastic search aggregation with range query ignored
  • Ship filebeat logs to logstash to index with docker metadata
  • How to get the SUM of the final score in an Elastic Search query_string request?
  • How to bypass filter if a tag is included?
  • The RestHighLevelClient cannot be used with elasticsearch 7 BulkProcessor. Which client should be used?
  • Can we use Elasticsearch SQL to insert or update data?
  • Elastic Search | How to get original search query with corresponding match value
  • Elasticsearch: getting documents with exactly a certain value in a field
  • Bucket selector in sub aggregation or cardinality aggregation
  • Elasticsearch query: range query on two fields, but one is optional field
  • use max/min aggregation on a geo_point field type
  • ElasticSearch parent/child on different indexes
  • Is there a way to exclude a field in an Elasticsearch query
  • How to update particular field in Elasticsearch index?
  • Summing nested documents in Elasticsearch
  • How to aggregate keyword field by a specific value in elasticsearch
  • How to activate and configure ElasticSearch Kafka Connect sink?
  • Making aggregations in two different types and return it grouped in Elasticsearch
  • what is purpose in must nested in filter elasticsearch?
  • Opendistro elasticsearch, no permissions for [ ] and User [name=admin, roles=[admin]
  • Find empty strings in elasticsearch
  • Implementing Array.Except(Array2) > 0 query in elasticsearch filter?
  • Elasticsearch count elements in array, for a property having a value
  • Read filebeat configration from Http URL?
  • Elasticsearch match nested field against array of values
  • Elasticsearch Primary Shards not balanced
  • How to tell ElasticSearch to create nested fields
  • ElasticSearch duplicating indexes
  • Issue while querying on a field that store a file path on ElasticSearch
  • How do we bucket aggregate only on the matched prefix on Elasticsearch?
  • Elasticsearch Completion Suggester - Sort suggestions
  • elasticsearch comparison between fields
  • Could not able to use geo_ip in logstash 2.4
  • ElasticSearch nGram filters out punctuation
  • Determining which words were matched in a fuzzy search
  • ES Time series data using Percentile/median
  • Writing queries in Kibana search bar
  • elasticsearch: update in a nested object (HTTP)
  • Joda Time - String to DateTime conversion
  • Request timedout during delete/create on elasticsearch while snapshot is being taken
  • query specific fields by document type
  • Elasticsearch - combining query_string and bool query in filter
  • How to create a bool filter on multiple fields in Elasticsearch?
  • Retrieve a document from elastic search by matching two fields
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com