Index on specific fields

Say we have 2 documents in the same bucket mybucket

{
doc_type = “doc1”
… other fields
}

{
doc_type=“doc2”
… other fields
}

We then make a
create primary index allDocs on mybucket using view;

and then using N1ql
SELECT * FROM allDocs where doc_type =‘doc1’ and we get all the documents with doc-type = doc 1

We would like to create a primary index on only specific documents that have doc_type = doc1
So we could do
SELECT * FROM doc1 and we get all the documents with doc-type = doc 1

Is this possible ? If yes please provide a simple example as i can’t get my head around it .

Any help is welcome .
George
Greece

That’s kind of possible I think, but it’s not a primary index and you can’t do a FROM index
Here is how to do it in N1QL/cbq command line client (if I understand what you want correctly):

CREATE INDEX ofTypeOne ON mybucket('doc-type') WHERE doc-type = "doc1";

Then all queries that constrain on the type being “doc1” would benefit from the index:

SELECT * FROM mybucket WHERE doc-type = "doc1";
--will simply take each row in the secondary index ofTypeOne

All queries above can be constructed via the DSL in the Java SDK, have a look at Index and Select classes respectively for starting points.

You can verify that it uses the index by prefixing your statement with EXPLAIN… If you want to force it, in the next dp the 2.2 SDK will include the notion of a hint, allowing to construct queries like the following with the DSL:

SELECT * FROM mybucket USE INDEX(ofTypeOne) WHERE doc-type = "doc1";
1 Like

@simonbasle Big Thanks !!!