Elasticsearch-dsl-py: term_vectors

Created on 24 May 2017  路  2Comments  路  Source: elastic/elasticsearch-dsl-py

Is there a nice way to get back _termvectors using DSL?

e.g. to replace this

GET /index/doc_type/ID/_termvectors?fields=text
{
    "term_statistics" : true,
    "field_statistics" : false,
    "positions": false,
    "offsets": false,
    "payloads":false,  
    "filter" : {
      "min_term_freq" : 2
    }
}

Or even better, directly in the search results.

Most helpful comment

Hi, exactly as @brainix said - the dsl only covers the most common use cases, for something like this, which I'd consider advanced functionality, please use the underlying elasticsearch-py library.

If you want to integrate this nicely with the DocType class you can create a subclass that adds this API as a method:

from elasticsearch_dsl import DocType
from elasticsearch_dsl.connections import connections

class TermVectorDocType(DocType):
    def termvectors(self, *fields, **kwargs):
        es = connections.get_connection(self._doc_type.using)
        return es.termvectors(
            index=self._get_index(),
            doc_type=self._doc_type.name,
            id=self.meta.id,
            fields=fields,
            **kwargs
        )

Hope this helps

All 2 comments

Hi, @eamonnmag!

AFAIK, there's no way to get term vectors back with search results. However, if you have a list of document IDs, you can fetch term vectors for all of those IDs using es.mtermvectors().

This is a case where I bypass elasticsearch-dsl-py and use elasticsearch-py straight.

I hope that helps!
Raj

Hi, exactly as @brainix said - the dsl only covers the most common use cases, for something like this, which I'd consider advanced functionality, please use the underlying elasticsearch-py library.

If you want to integrate this nicely with the DocType class you can create a subclass that adds this API as a method:

from elasticsearch_dsl import DocType
from elasticsearch_dsl.connections import connections

class TermVectorDocType(DocType):
    def termvectors(self, *fields, **kwargs):
        es = connections.get_connection(self._doc_type.using)
        return es.termvectors(
            index=self._get_index(),
            doc_type=self._doc_type.name,
            id=self.meta.id,
            fields=fields,
            **kwargs
        )

Hope this helps

Was this page helpful?
0 / 5 - 0 ratings

Related issues

takaomag picture takaomag  路  3Comments

abuzakaria picture abuzakaria  路  4Comments

SalahAdDin picture SalahAdDin  路  4Comments

MauriJHN picture MauriJHN  路  4Comments

amih90 picture amih90  路  4Comments