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


Python IndicesClient.get_alias方法代码示例

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


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

示例1: main

# 需要导入模块: from elasticsearch.client import IndicesClient [as 别名]
# 或者: from elasticsearch.client.IndicesClient import get_alias [as 别名]
def main(n_indexes):
    client = Elasticsearch('localhost:9200')
    index_client = IndicesClient(client)

    index = 'image_hashes_01'

    if n_indexes < 1:
        search_index = 'image_hashes*'
    else:
        search_index = ','.join('image_hashes_%02d' % i for i in range(2,n_indexes+2))

        aliases = set(index_client.get_alias(index='image_hashes_*').keys())
        for tmp in search_index.split(','):
            if tmp not in aliases:
                search_index = 'image_hashes*'
                break

    print('Searching from ' + search_index)
    
    field_sort = lambda: {'%x' % randint(0, n_samples-1): 'asc'}
    query = {
        'size': 500,
        'sort': [field_sort() for i in range(4)],
        'fields': ['raw']
    }
    #print(json.dumps(query, indent=4, sort_keys=True)); return
    
    docs = [
        doc['fields']['raw'][0]
        for doc in client.search(
            index=index, body=query, request_timeout=1e3
        )['hits']['hits']
    ]
    n_docs = len(docs)
    
    #print('\n'.join(docs[0:10])); return
    
    durations = ([], [])
    matches   = ([], [])
    
    for i in range(n_docs):
        doc = np.array([int(i) for i in docs[i][::-1]]).astype(np.uint64)        
        doc = {'%x' % j:
            int(doc.dot(sampler[:,:,j]).dot(pow2).astype(np.int16))
            for j in range(n_samples)
        }

        #print(json.dumps(doc, indent=2, sort_keys=True)); return

        query = [None, None]

        query[0] = {
            'bool': {
                'should': [
                    {
                    'filtered': {
                        'filter': {
                                'term': {
                                    field: doc[field],
                                    '_cache': False
                                }
                            }
                        }
                    }
                    for field in sorted(doc.keys())
                ],
                'minimum_should_match': 29
            }
        }
        
        query[1] = {
            'fuzzy': {
                'raw': {
                    'value': docs[i],
                    'fuzziness': 2,
                    'max_expansions': 1000000
                }
            }
        }

        #print(json.dumps(query, indent=2)); return
        
        for j in range(len(query)):
            start = time.time()
            n_hits = client.search(index=search_index, body={
                'size': 0,
                'query': query[j]
            }, request_timeout=600)['hits']['total']

            durations[j].append(1000*(time.time() - start))
            matches[j].append(n_hits)

        print('\r%4d/%d %5d %7.2f ms, %5d %7.2f ms' % (i+1, len(docs),
            matches[0][-1], durations[0][-1],
            matches[1][-1], durations[1][-1]
        ), end='')
        sys.stdout.flush()
    
    print('\n\nMean matches: %.3f / %.3f' % (sum(matches[0]) / n_docs, sum(matches[1]) / n_docs))
    print('Mean time:    %.3f / %.3f ms' % (sum(durations[0]) / n_docs, sum(durations[1]) / n_docs))
#.........这里部分代码省略.........
开发者ID:VisBlank,项目名称:stackoverflow-scripts,代码行数:103,代码来源:benchmark_2.py

示例2: IndicesManager

# 需要导入模块: from elasticsearch.client import IndicesClient [as 别名]
# 或者: from elasticsearch.client.IndicesClient import get_alias [as 别名]
class IndicesManager(object):

    def __init__(self, options=None):
        self.options = options or {}
        self.es = get_elasticsearch(self.options)
        self.esc = IndicesClient(self.es)
        self.conf_dir = sys.path[0]

    def __create__(self, name, config=None, type=None):
        result = None

        try:
            if not config:
                file_name = "{}/config/{}_index.json".format(
                    self.conf_dir, type)
                with open(file_name) as fp:
                    config = fp.read()

            # create the index with version number
            result = self.esc.create(index=name, body=config)

        except es_exceptions.TransportError:
            print("unable to connect to Elasticsearch")

        return result

    def create(self, doc_type):
        alias_name = 'frisc_{}'.format(doc_type)
        index_name = '{}_v1'.format(alias_name)

        try:
            if self.esc.exists_alias(alias_name):
                print('Index {} already existst, updating'.format(alias_name))
                self.update(doc_type)
                return

            self.__create__(index_name, type=doc_type)

            # set an alias to the index
            self.esc.put_alias(index=index_name, name=alias_name)

        except es_exceptions.TransportError:
            print("unable to connect to Elasticsearch")

    def update(self, doc_type):
        alias_name = 'frisc_{}'.format(doc_type)
        index_name = '{}_v1'.format(alias_name),

        try:
            if not self.esc.exists_alias(alias_name):
                self.create(doc_type)
                return

            version_number = 0
            old_index_name = ''

            old_indexes = self.esc.get_alias(name=alias_name)
            for index in old_indexes.keys():
                match = re.search('^({})_v(\d+)$'.format(alias_name), index)
                if match:
                    version = int(match.group(2))
                    if version > version_number:
                        version_number = version
                        old_index_name = match.group(0)

            version_number += 1
            index_name = '{}_v{}'.format(alias_name, version_number)

            if self.esc.exists(index_name):
                # raise soemthing
                raise

            self.__create__(index_name, type=doc_type)

            reindex(self.es, old_index_name, index_name)

            self.esc.update_aliases(
                body={'actions': [
                    {'remove': {'alias': alias_name, 'index': old_index_name}},
                    {'add': {'alias': alias_name, 'index': index_name}}
                ]}
            )

        except es_exceptions.TransportError:
            print("unable to connect to Elasticsearch")
开发者ID:zuloo,项目名称:frisc,代码行数:87,代码来源:index.py


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