Check if the Primary Index is already created

  1. How can i check if the Primary index is already created [N1ql Query]?, if not created then only i want to create it.
    I first wanted to check, as i am getting, “Query error: [{“msg”:“GSI CreatePrimaryIndex() - cause: Index #primary already exists.”,“code”:5000}]” Error.

  2. How can i check if a specific document key is already present in the bucket?
    I have tried "SELECT RAW META().id FROM bucketName"
    This gives me list of all document key’s in the bucket, is there a way i can check for a specific document key instead of getting an array of document keys present in the bucket?

SELECT RAW META().id FROM bucketName WHERE META().id = "XXXX";
OR
SELECT RAW META().id FROM bucketName  USE KEYS ["XXXX"]

you can get index info from system:indexes
FYI , for example,

SELECT * FROM system:indexes

how about

SELECT RAW META().id FROM bucketName USE KEYS ["key1","key2"]

as @vsr1 suggested.

I will try as you suggested, Thank you.

String kQueryStr = “SELECT RAW META().id FROM" + bucket.name() + "WHERE META().id = Rakesh”;
N1qlQuery kQuery = N1qlQuery.simple(kQueryStr);
N1qlQueryResult kQueryResult = bucket.query(kQuery);

I have tried this query to check if the document named “Rakesh” is present or not, even though the specific document is not present it shows the query is successful.

N1qlQueryResult{status=‘success’, finalSuccess=true, parseSuccess=true, allRows=[], signature=json, info=N1qlMetrics{resultCount=0, errorCount=0, warningCount=0, mutationCount=0, sortCount=0, resultSize=0, elapsedTime=‘2.0059ms’, executionTime=‘2.0059ms’}, profileInfo={}, errors=[], requestId=‘1216ab62-9f26-4236-97ec-135263ce0c01’, clientContextId=’’}

is there anything wrong with the query i am running?

Nothing wrong. Query Execution is Successfully completed. resultCount=0 and AlllRows is empty array. You need to check those fields if there are any results or not.

Also META().id needs to be string. Rakesh needs to be embedded in the quotes.

Understood.
Thank you.