当前位置: 首页>>代码示例>>Python>>正文


Python HighLighter.add_field方法代码示例

本文整理汇总了Python中highlight.HighLighter.add_field方法的典型用法代码示例。如果您正苦于以下问题:Python HighLighter.add_field方法的具体用法?Python HighLighter.add_field怎么用?Python HighLighter.add_field使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在highlight.HighLighter的用法示例。


在下文中一共展示了HighLighter.add_field方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Search

# 需要导入模块: from highlight import HighLighter [as 别名]
# 或者: from highlight.HighLighter import add_field [as 别名]
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.
#.........这里部分代码省略.........
开发者ID:ieure,项目名称:pyes,代码行数:103,代码来源:query.py

示例2: Query

# 需要导入模块: from highlight import HighLighter [as 别名]
# 或者: from highlight.HighLighter import add_field [as 别名]
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)
开发者ID:dlg,项目名称:pyes,代码行数:83,代码来源:query.py


注:本文中的highlight.HighLighter.add_field方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。