Elasticsearch-php: Existence of quantity?

Created on 25 Nov 2020  路  2Comments  路  Source: elastic/elasticsearch-php

How can I know if a desired value exists or not?

question

Most helpful comment

What do you mean about a "desiderad value"?

If you mean if an index exists in Elasticsearch you can use the indices.exists API, as follows:

$index = 'insert-index-name';
$result = $client->indices()->exists([ 'index' => $index ]);
printf ("Index %s %s", $index, $result ? 'exists' : 'does not exist');

Instead, if you mean if a document ID exists, you can use the exists API, as follows:

$index = 'insert-index-name';
$documentId = 'insert-document-id';
$result = $client->exists([ 
    'index' => $index,
    'id' => $documentId
]);
printf (
    "Document ID %s with index %s %s", 
    $index,
    $documentId,
    $result ? 'exists' : 'does not exist'
);

All 2 comments

What do you mean about a "desiderad value"?

If you mean if an index exists in Elasticsearch you can use the indices.exists API, as follows:

$index = 'insert-index-name';
$result = $client->indices()->exists([ 'index' => $index ]);
printf ("Index %s %s", $index, $result ? 'exists' : 'does not exist');

Instead, if you mean if a document ID exists, you can use the exists API, as follows:

$index = 'insert-index-name';
$documentId = 'insert-document-id';
$result = $client->exists([ 
    'index' => $index,
    'id' => $documentId
]);
printf (
    "Document ID %s with index %s %s", 
    $index,
    $documentId,
    $result ? 'exists' : 'does not exist'
);

thanks

Was this page helpful?
0 / 5 - 0 ratings