예제 #1
0
 def post_json(self):
     # NOTE: this is same basic code as Docstore.index
     return docstore.Docstore().post(
         self,
         docstore._public_fields().get(self.identifier.model, []), {
             'parent_id': self.identifier.parent_id(),
         })
예제 #2
0
def publish(hosts, index, recurse, force, path):
    """Post the document and its children to Elasticsearch
    """
    status = docstore.Docstore(hosts, index).post_multi(path,
                                                        recursive=recurse,
                                                        force=force)
    click.echo(status)
예제 #3
0
def create(hosts, index):
    """Create new index.
    """
    try:
        docstore.Docstore(hosts, index).create_index(index)
    except Exception as err:
        logprint('error', err)
예제 #4
0
def get(hosts, index, json, pretty, doctype, object_id):
    """Pretty-print a single document
    """
    document = docstore.Docstore(hosts, index).get(doctype, object_id)
    if json:
        click.echo(format_json(document.to_dict(), pretty=pretty))
    else:
        click.echo(document)
예제 #5
0
def vocabs(hosts, index, path):
    """Post DDR vocabulary facets and terms.
    
    \b
    Example:
      $ ddrindex vocabs /opt/ddr-local/ddr-vocab/api/0.2/
    """
    docstore.Docstore(hosts, index).post_vocabs(path=path)
예제 #6
0
def status(hosts, index):
    """Print status info.
    
    More detail since you asked.
    """
    ds = docstore.Docstore(hosts, index)
    s = ds.status()

    logprint(
        'debug',
        '------------------------------------------------------------------------',
        0)
    logprint('debug', 'Elasticsearch', 0)
    # config file
    logprint('debug', 'DOCSTORE_HOST  (default): %s' % config.DOCSTORE_HOST, 0)
    logprint('debug', 'DOCSTORE_INDEX (default): %s' % config.DOCSTORE_INDEX,
             0)
    # overrides
    if hosts != config.DOCSTORE_HOST:
        logprint('debug', 'docstore_hosts: %s' % hosts, 0)
    if index != config.DOCSTORE_INDEX:
        logprint('debug', 'docstore_index: %s' % index, 0)

    try:
        pingable = ds.es.ping()
        if not pingable:
            logprint('error', "Can't ping the cluster!", 0)
            return
    except elasticsearch.exceptions.ConnectionError:
        logprint('error', "Connection error when trying to ping the cluster!",
                 0)
        return
    logprint('debug', 'ping ok', 0)

    logprint('debug', 'Indexes', 0)
    index_names = ds.es.indices.stats()['indices'].keys()
    for i in index_names:
        if i == index:
            logprint('debug', '* %s *' % i, 0)
        else:
            logprint('debug', '- %s' % i, 0)

    logprint('debug', 'Aliases', 0)
    aliases = ds.aliases()
    if aliases:
        for index, alias in aliases:
            logprint('debug', '- %s -> %s' % (alias, index), 0)
    else:
        logprint('debug', 'No aliases', 0)

    if ds.es.indices.exists(index=index):
        logprint('debug', 'Index %s present' % index, 0)
    else:
        logprint('error', "Index '%s' doesn't exist!" % index, 0)
        return

    # TODO get ddrindex status model counts to work
    logprint('debug', '(Object counts are currently unavailable)', 0)
예제 #7
0
def delete(hosts, index, recurse, confirm, doctype, object_id):
    """Delete the specified document from Elasticsearch
    """
    if confirm:
        click.echo(
            docstore.Docstore(hosts, index).delete(object_id,
                                                   recursive=recurse))
    else:
        click.echo("Add '--confirm' if you're sure you want to do this.")
예제 #8
0
def alias(hosts, index, alias, delete):
    """Manage aliases.
    """
    if not alias:
        click.echo("Error: no alias specified.")
        return
    if delete:
        try:
            docstore.Docstore(hosts, index).delete_alias(index=index,
                                                         alias=alias)
        except Exception as err:
            logprint('error', err)
    else:
        try:
            docstore.Docstore(hosts, index).create_alias(index=index,
                                                         alias=alias)
        except Exception as err:
            logprint('error', err)
예제 #9
0
def search(hosts, index, doctypes, query, must, should, mustnot, raw):
    """
    """
    click.echo(
        search_results(d=docstore.Docstore(hosts, index),
                       doctype=doctypes,
                       text=query,
                       must=must,
                       should=should,
                       mustnot=mustnot,
                       raw=raw))
예제 #10
0
def postjson(hosts, index, doctype, object_id, path):
    """Post raw JSON file to Elasticsearch (YMMV)
    
    This command is for posting raw JSON files.  If the file you wish to post
    is a DDR object, please use "ddrindex post".
    """
    with open(path, 'r') as f:
        text = f.read()
    status = docstore.Docstore(hosts, index).post_json(doctype, object_id,
                                                       text)
    click.echo(status)
예제 #11
0
def destroy(hosts, index, confirm):
    """Delete index (requires --confirm).
    
    \b
    It's meant to sound serious. Also to not clash with 'delete', which
    is for individual documents.
    """
    if confirm:
        try:
            docstore.Docstore(hosts, index).delete_index()
        except Exception as err:
            logprint('error', err)
    else:
        click.echo("Add '--confirm' if you're sure you want to do this.")
예제 #12
0
 def reindex(self):
     """Reindex Collection objects to Elasticsearch
     """
     ds = docstore.Docstore(config.DOCSTORE_HOST, config.DOCSTORE_INDEX)
     # check for ES connection before going to all the trouble
     health = ds.health()
     index_exists = ds.index_exists(config.DOCSTORE_INDEX)
     if not index_exists:
         return {
             'error':
             'Missing Elasticsearch index "%s"' % config.DOCSTORE_INDEX
         }
     return ds.post_multi(self.identifier.path_abs(),
                          recursive=True,
                          force=True)
예제 #13
0
def exists(hosts, index, doctype, object_id):
    """Indicate whether the specified document exists
    """
    ds = docstore.Docstore(hosts, index)
    click.echo(ds.exists(doctype, object_id))
예제 #14
0
def mappings(hosts, index):
    """Push mappings to the specified index.
    """
    docstore.Docstore(hosts, index).init_mappings()
예제 #15
0
def conf(hosts, index):
    """Print configuration settings.
    
    More detail since you asked.
    """
    docstore.Docstore(hosts, index).print_configs()