Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Getting empty results #17

Open
kdbusiness90 opened this issue Dec 25, 2017 · 9 comments
Open

Getting empty results #17

kdbusiness90 opened this issue Dec 25, 2017 · 9 comments

Comments

@kdbusiness90
Copy link

@myarik, I am new to elastic search.

I followed documentation.

curl 'localhost:9200/_cat/indices?v'

health status index                       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .monitoring-data-2          0jdQXTkYRwGeJMOcHAv6-g   1   1          3            0    312.8kb        156.4kb
green  open   .monitoring-es-2-2017.12.25 F257mV7HR12YWmVd29mMWg   1   1      21661           14     25.4mb         12.6mb
green  open   notes                       SR2jPX-tR06rHw66AVXtkg   5   1          0            0      1.2kb           650b

notes index has doc.count = 0 which is wrong.(Pls correct me)

Notes model has 17 records.
screenshot from 2017 12 25 16 48 48

View.py


class SearchView(es_views.ListElasticAPIView):
    es_client = ES_CLIENT
    es_model = NoteIndex
    es_pagination_class = es_pagination.ElasticLimitOffsetPagination
    es_filter_backends = (es_filters.ElasticFieldsFilter, es_filters.ElasticFieldsRangeFilter,
                          es_filters.ElasticSearchFilter, es_filters.ElasticOrderingFilter)
    es_ordering = 'created_at'
    es_range_filter_fields = (es_filters.ESFieldFilter('created_at', 'created_at'),)
    es_search_fields = ('description', 'subject',)

search_indexes.py


class NoteIndex(DocType):
    """
    NoteIndex.init(using=es_client)
    """
    pk = Integer()
    description = Text(fields={'raw': Keyword()})
    created_at = Date()
    subject = Text()

    class Meta:
        index = 'notes'

NoteIndex.init(using=ES_CLIENT)

curl http://localhost:8000/app/api/Search?search=milk

{
    "count": 0,
    "next": null,
    "previous": null,
    "results": []
}

Configuration

  • Operating system version: Ubuntu 14.04.5 LTS
  • elasticsearch:5.2.2
  • djangorestframework==3.7.7
  • django-rest-elasticsearch==0.3.3
  • elasticsearch-dsl==5.4.0
  • elasticsearch==5.5.1
@sillyfatcat
Copy link

Seems like you don't have an ElasticSearch Serializer class and the model signal to handle this. Could you verify you have both ES serializers and the appropriate signals hooked to the correct models?

@kdbusiness90
Copy link
Author

kdbusiness90 commented Dec 27, 2017

@sshum00, Thanks, it is working.

@kdbusiness90
Copy link
Author

hello @sshum00,

how to achieve request based ordering ?

Currently I have tried below request:

http://localhost:8000/app/api/Search?es_ordering=-modified_at&limit=10&offset=10&page=1&search=some

View.py

class SearchView(es_views.ListElasticAPIView):
    es_client = ES_CLIENT
    es_model = NotesIndex
    es_pagination_class = ElasticResultsSetPagination
    es_filter_backends = (es_filters.ElasticFieldsFilter, es_filters.ElasticFieldsRangeFilter,
                          es_filters.ElasticSearchFilter, es_filters.ElasticOrderingFilter)
    es_ordering_fields = ("__all__",)
    es_ordering = '-modified_at'
    es_search_fields = ('description', 'subject',)

@sillyfatcat
Copy link

sillyfatcat commented Dec 31, 2017

If you want the request to allow it to modify the ordering modify the variable es_ordering based on the request param. Unfortunately there's no get_es_ordering method in the ListElasticAPIView that you could overload from a quick glance. So without modifying the package, the next easiest way is to create a class that extends es_views.ListElasticAPIView and add that method. Or set the es_ordering variable a dynamic class property like so.

This is untested but if you're willing to go through this:

class SearchView(es_views.ListElasticAPIView):
    ...
    all your other stuff except for es_ordering 
    ...
    @property
    def es_ordering(self):
        if self.request.GET.get('ordering'):
            return self.request.GET.get('es_ordering') #this is whatever URL variable you decide to use
       return 'somedefaultfield'

You could do something like this for the least amount of work. Though I can't guarantee if it'll work as I haven't tested it.

@kdbusiness90
Copy link
Author

@sshum00, I have tried above solution.

Getting below error:

RequestError: TransportError(400, u'search_phase_execution_exception', u'Fielddata is disabled on text fields by default. Set fielddata=true on [subject] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.')

view.py


class SearchView(es_views.ListElasticAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    es_client = ES_CLIENT
    es_model = NotesIndex
    es_pagination_class = ElasticResultsSetPagination
    es_filter_backends = (es_filters.ElasticFieldsFilter, es_filters.ElasticFieldsRangeFilter,
                          es_filters.ElasticSearchFilter, es_filters.ElasticOrderingFilter)
    es_ordering_fields = ("description", "subject", "-modified_at", "filing_name")
    es_search_fields = ('description', 'subject', 'filing_name')

    @property
    def es_ordering(self):
        return self.request.GET.get('es_ordering') or '-modified_at'

@sillyfatcat
Copy link

Without knowing more about your system it would be hard to tell what went wrong with this. It doesn't appear to be specific to the newly added code. The most obvious thing to look for I suppose is: do you have enough memory?

@kdbusiness90
Copy link
Author

kdbusiness90 commented Dec 31, 2017

@sshum00, I need to achieve fielddata:true in mapping.

Please refer below links

https://stackoverflow.com/questions/38145991/how-to-set-fielddata-true-in-kibana

I have tried this solution but not working.
elastic/elasticsearch-dsl-py#637

@kdbusiness90 kdbusiness90 reopened this Dec 31, 2017
@kdbusiness90
Copy link
Author

@sshum00, I got solution for above problem. I was trying sort on fields, so It worked es_ordering=subject.keyword.

but now I am facing issue with es_filter_fields

view.py


class SearchView(es_views.ListElasticAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    es_client = ES_CLIENT
    es_model = NotesIndex
    es_pagination_class = ElasticResultsSetPagination
    es_filter_backends = (es_filters.ElasticFieldsFilter, es_filters.ElasticFieldsRangeFilter,
                          es_filters.ElasticSearchFilter, es_filters.ElasticOrderingFilter)
    es_filter_fields = (
        es_filters.ESFieldFilter('title_filing', 'title_filing'),
    )
    es_ordering_fields = ("description", "subject", "-modified_at", "filing_name")
    es_search_fields = ('description', 'subject', 'filing_name')

    @property
    def es_ordering(self):
        return self.request.GET.get('es_ordering') or '-modified_at'

search_indexes.py


class NotesIndex(DocType):
    """
    NoteIndex.init(using=es_client)
    """
    pk = Integer()
    description = Text(fields={'raw': Keyword()}, fielddata=True)
    modified_at = Date()
    subject = Text(fielddata=True)
    filing_name = Text(fielddata=True)

    class Meta:
        index = 'notes'

curl http://localhost:8000/app/api/Search?es_ordering=-modified_at&filing_id=&limit=10&offset=0&page=0&search=some&title_filing=Do

Which is not filtering based on title_filing

@sillyfatcat
Copy link

That field does not appear to be part of your serializer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants