Exemple #1
0
    def add_highlight(self, field, fragment_size=None,
                      number_of_fragments=None, fragment_offset=None):
        """Add a highlight field.

        The Search object will be returned, so calls to this can be chained.

        """
        if self.highlight is None:
            self.highlight = HighLighter("<b>", "</b>")
        self.highlight.add_field(field, fragment_size, number_of_fragments, fragment_offset)
        return self
Exemple #2
0
 def add_highlight(self, field, fragment_size=None, number_of_fragments=None):
     """
     Add an highlight field
     """
     if self.highlight is None:
         self.highlight = HighLighter("<b>", "</b>")
     self.highlight.add_field(field, fragment_size, number_of_fragments)
Exemple #3
0
    def add_highlight(self, field, fragment_size=None, number_of_fragments=None):
        """Add a highlight field.

        The Search object will be returned, so calls to this can be chained.

        """
        if self.highlight is None:
            self.highlight = HighLighter("<b>", "</b>")
        self.highlight.add_field(field, fragment_size, number_of_fragments)
        return self
Exemple #4
0
class Search(object):
    """A search to be performed.

    This contains a query, and has additional parameters which are used to
    control how the search works, what it should return, etc.

    """
    def __init__(self,
                 query=None,
                 fields=None,
                 start=None,
                 size=None,
                 highlight=None,
                 sort=None,
                 explain=False,
                 facet=None,
                 version=None,
                 track_scores=None,
                 index_boost={}):
        """
        fields: if is [], the _source is not returned
        """
        self.query = query
        self.fields = fields
        self.start = start
        self.size = size
        self.highlight = highlight
        self.sort = sort
        self.explain = explain
        self.facet = facet or FacetFactory()
        self.version = version
        self.track_scores = track_scores
        self.index_boost = index_boost

    def get_facet_factory(self):
        """
        Returns the facet factory
        """
        return self.facet

    @property
    def q(self):
        return self.serialize()

    def serialize(self):
        """Serialize the search to a structure as passed for a search body.

        """
        res = {"query": self.query.serialize()}
        if self.fields is not None:
            res['fields'] = self.fields
        if self.size is not None:
            res['size'] = self.size
        if self.start is not None:
            res['from'] = self.start
        if self.highlight:
            res['highlight'] = self.highlight.serialize()
        if self.sort:
            res['sort'] = self.sort
        if self.explain:
            res['explain'] = self.explain
        if self.version:
            res['version'] = self.version
        if self.track_scores:
            res['track_scores'] = self.track_scores
        if self.index_boost:
            res['indices_boost'] = self.index_boost
        if self.facet.facets:
            res.update(self.facet.q)
        return res

    def add_highlight(self,
                      field,
                      fragment_size=None,
                      number_of_fragments=None):
        """Add a highlight field.

        The Search object will be returned, so calls to this can be chained.

        """
        if self.highlight is None:
            self.highlight = HighLighter("<b>", "</b>")
        self.highlight.add_field(field, fragment_size, number_of_fragments)
        return self

    def add_index_boost(self, index, boost):
        """Add a boost on an index.

        The Search object will be returned, so calls to this can be chained.

        """
        if boost is None:
            if self.index_boost.has_key(index):
                del (self.index_boost[index])
        else:
            self.index_boost[index] = boost
        return self

    def __repr__(self):
        return str(self.q)

    def to_search_json(self):
        """Convert the search to JSON.

        The output of this is suitable for using as the request body for
        search.

        """
        return json.dumps(self.q, cls=ESJsonEncoder)
Exemple #5
0
class Search(object):
    """A search to be performed.

    This contains a query, and has additional parameters which are used to
    control how the search works, what it should return, etc.

    """
    def __init__(self,
                 query=None,
                 filter=None,
                 fields=None,
                 start=None,
                 size=None,
                 highlight=None,
                 sort=None,
                 explain=False,
                 facet=None,
                 version=None,
                 track_scores=None,
                 script_fields=None,
                 index_boost={},
                 min_score=None):
        """
        fields: if is [], the _source is not returned
        """
        self.query = query
        self.filter = filter
        self.fields = fields
        self.start = start
        self.size = size
        self.highlight = highlight
        self.sort = sort
        self.explain = explain
        self.facet = facet or FacetFactory()
        self.version = version
        self.track_scores = track_scores
        self.script_fields = script_fields
        self.index_boost = index_boost
        self.min_score = min_score

    def get_facet_factory(self):
        """
        Returns the facet factory
        """
        return self.facet

    @property
    def q(self):
        return self.serialize()

    def serialize(self):
        """Serialize the search to a structure as passed for a search body.

        """
        res = {"query": self.query.serialize()}
        if self.filter:
            res['filter'] = self.filter.serialize()
        if self.fields is not None:
            res['fields'] = self.fields
        if self.size is not None:
            res['size'] = self.size
        if self.start is not None:
            res['from'] = self.start
        if self.highlight:
            res['highlight'] = self.highlight.serialize()
        if self.sort:
            res['sort'] = self.sort
        if self.explain:
            res['explain'] = self.explain
        if self.version:
            res['version'] = self.version
        if self.track_scores:
            res['track_scores'] = self.track_scores
        if self.script_fields:
            if isinstance(self.script_fields, ScriptFields):
                res['script_fields'] = self.script_fields.serialize()
            else:
                raise ScriptFieldsError("Parameter script_fields should of type ScriptFields")
        if self.index_boost:
            res['indices_boost'] = self.index_boost
        if self.min_score:
            res['min_score'] = self.min_score
        if self.facet.facets:
            res.update(self.facet.q)
        return res

    def add_highlight(self, field, fragment_size=None,
                      number_of_fragments=None, fragment_offset=None):
        """Add a highlight field.

        The Search object will be returned, so calls to this can be chained.

        """
        if self.highlight is None:
            self.highlight = HighLighter("<b>", "</b>")
        self.highlight.add_field(field, fragment_size, number_of_fragments, fragment_offset)
        return self

    def add_index_boost(self, index, boost):
        """Add a boost on an index.

        The Search object will be returned, so calls to this can be chained.

        """
        if boost is None:
            if self.index_boost.has_key(index):
                del(self.index_boost[index])
        else:
            self.index_boost[index] = boost
        return self

    def __repr__(self):
        return str(self.q)

    def to_search_json(self):
        """Convert the search to JSON.

        The output of this is suitable for using as the request body for
        search.

        """
        return json.dumps(self.q, cls=ESJsonEncoder)
Exemple #6
0
 def highlight(self):
     if self._highlight is None:
         self._highlight = HighLighter("<b>", "</b>")
     return self._highlight
Exemple #7
0
class Query(object):
    def __init__(self,
                 fields=None,
                 start=None,
                 size=None,
                 highlight=None,
                 sort=None,
                 explain=False,
                 facet=None,
                 index_boost={}):
        """
        fields: if is [], the _soruce is not returned
        """
        self.fields = fields
        self.start = start
        self.size = size
        self.highlight = highlight
        self.sort = sort
        self.explain = explain
        self.facet = facet or FacetFactory()
        self.index_boost = index_boost

    def get_facet_factory(self):
        """
        Returns the facet factory
        """
        return self.facet

    @property
    def q(self):
        res = {"query":self.serialize()}
        if self.fields is not None:
            res['fields'] = self.fields
        if self.size is not None:
            res['size'] = self.size
        if self.start is not None:
            res['from'] = self.start
        if self.highlight:
            res['highlight'] = self.highlight.serialize()
        if self.sort:
            res['sort'] = self.sort
        if self.explain:
            res['explain'] = self.explain
        if self.index_boost:
            res['indices_boost'] = self.index_boost
        if self.facet.facets:
            res.update(self.facet.q)
        return res

    def add_highlight(self, field, fragment_size=None, number_of_fragments=None):
        """
        Add an highlight field
        """
        if self.highlight is None:
            self.highlight = HighLighter("<b>", "</b>")
        self.highlight.add_field(field, fragment_size, number_of_fragments)

    def add_index_boost(self, index, boost):
        """
        Add a boost on an index
        """
        if boost is None:
            if self.index_boost.has_key(index):
                del(self.index_boost[index])
        else:
            self.index_boost[index] = boost

    def count(self):
        return self.serialize()

    def __repr__(self):
        return str(self.q)

    def to_json(self, inner=False):
        """
        Inner return only the inner query. Useful for delete_by_query and reindex.
        """
        q = self.q
        if inner:
            q = q['query']
        return json.dumps(q, cls=ESJsonEncoder)