1010import java .util .Map ;
1111
1212public class FindOne {
13- public static void main (String [] args ) {
13+ public static void main (String [] args ) {
14+ AstraDB db = new AstraDB ("<token>" , "<api_endpoint>" );
15+ AstraDBCollection collection = db .createCollection ("collection_vector1" , 14 );
1416
15- // Accessing existing DB
16- AstraDB db = new AstraDB ("<token>" , "<api_endpoint>" );
17+ // Retrieve first document where product_price exists
18+ collection .findOne (SelectQuery .builder ()
19+ .where ("product_price" )
20+ .exists ().build ())
21+ .ifPresent (System .out ::println );
1722
18- // Access existing collection
19- AstraDBCollection collection = db .createCollection ("collection_vector1" , 14 );
23+ // Retrieve first document where product_price is 12.99
24+ collection .findOne (SelectQuery .builder ()
25+ .where ("product_price" )
26+ .isEqualsTo (12.99 ).build ())
27+ .ifPresent (System .out ::println );
2028
21- // Retrieve first document where product_price exists
22- collection .findOne (SelectQuery .builder ()
23- .where ("product_price" )
24- .exists ().build ())
25- .ifPresent (System .out ::println );
29+ // Retrieve first document where product_price is 12.99 and product_name is "HealthyFresh - Beef raw dog food"
30+ collection .findOne (SelectQuery .builder ()
31+ .where ("product_name" ).isEqualsTo ("HealthyFresh - Chicken raw dog food" )
32+ .andWhere ("product_price" )
33+ .isEqualsTo (9.99 ).build ())
34+ .ifPresent (System .out ::println );
2635
27- // Retrieve first document where product_price is 12.99
28- collection .findOne (SelectQuery .builder ()
29- .where ("product_price" )
30- .isEqualsTo (12.99 ).build ())
31- .ifPresent (System .out ::println );
36+ // Send the request as a JSON String
37+ collection .findOne ("{" +
38+ "\" filter\" :{" +
39+ "\" product_price\" :9.99," +
40+ "\" product_name\" :\" HealthyFresh - Chicken raw dog food\" }" +
41+ "}" )
42+ .ifPresent (System .out ::println );
3243
33- // Retrieve first document where product_price is 12.99 and product_name is "HealthyFresh - Beef raw dog food"
34- collection .findOne (SelectQuery .builder ()
35- .where ("product_name" ).isEqualsTo ("HealthyFresh - Chicken raw dog food" )
36- .andWhere ("product_price" ).isEqualsTo (9.99 ).build ())
37- .ifPresent (System .out ::println );
44+ // Limit retrieved fields to product_name and product_price
45+ collection .findOne (SelectQuery .builder ()
46+ .select ("product_name" , "product_price" )
47+ .where ("product_price" )
48+ .isEqualsTo (9.99 )
49+ .build ())
50+ .ifPresent (System .out ::println );
3851
39- // Send the request as a JSON String
40- collection .findOne ("{" +
41- " \" filter \" :{" +
42- " \" product_price \" : 9.99, \" product_name \" : \" HealthyFresh - Chicken raw dog food \" }" +
43- "}" )
44- . ifPresent ( System . out :: println );
52+ // Add an Ann Search
53+ collection .findOne (SelectQuery . builder ()
54+ . where ( "product_price" )
55+ . isEqualsTo ( 9.99 )
56+ . orderByAnn ( new float []{ 1f , 0f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f } )
57+ . build () );
4558
46- // Limit retrieved fields to product_name and product_price
47- collection
48- .findOne (SelectQuery .builder ()
49- .select ("product_name" , "product_price" )
50- .where ("product_price" )
51- .isEqualsTo (9.99 )
52- .build ())
53- .ifPresent (System .out ::println );
59+ // Complex query with AND and OR
60+ SelectQuery sq2 = new SelectQuery ();
61+ sq2 .setFilter (new HashMap <>());
62+ Map <String , List <Map <String , Object >>> or1Criteria = new HashMap <>();
63+ or1Criteria .put ("$or" , new ArrayList <Map <String , Object >>());
64+ or1Criteria .get ("$or" ).add (Map .of ("product_price" , 9.99 ));
65+ or1Criteria .get ("$or" ).add (Map .of ("product_name" , "HealthyFresh - Beef raw dog food" ));
66+ Map <String , List <Map <String , Object >>> or2Criteria = new HashMap <>();
67+ or2Criteria .put ("$or" , new ArrayList <Map <String , Object >>());
68+ or2Criteria .get ("$or" ).add (Map .of ("product_price" , 12.99 ));
69+ or2Criteria .get ("$or" ).add (Map .of ("product_name" , "HealthyFresh - Beef raw dog food" ));
70+ List <Map <String , List <Map <String , Object >>>> andCriteria = new ArrayList <>();
71+ andCriteria .add (or1Criteria );
72+ andCriteria .add (or2Criteria );
73+ sq2 .getFilter ().put ("$and" , andCriteria );
74+ collection .findOne (sq2 ).ifPresent (System .out ::println );
5475
55- // Add an Ann Search
56- collection .findOne (SelectQuery
57- .builder ()
58- .where ("product_price" )
59- .isEqualsTo (9.99 )
60- .orderByAnn (new float []{1f , 0f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f })
61- .build ());
62-
63- // Complex query with AND and OR
64- SelectQuery sq2 = new SelectQuery ();
65- sq2 .setFilter (new HashMap <>());
66- Map <String , List <Map <String , Object >>> or1Criteria = new HashMap <>();
67- or1Criteria .put ("$or" , new ArrayList <Map <String , Object >>());
68- or1Criteria .get ("$or" ).add (Map .of ("product_price" , 9.99 ));
69- or1Criteria .get ("$or" ).add (Map .of ("product_name" , "HealthyFresh - Beef raw dog food" ));
70- Map <String , List <Map <String , Object >>> or2Criteria = new HashMap <>();
71- or2Criteria .put ("$or" , new ArrayList <Map <String , Object >>());
72- or2Criteria .get ("$or" ).add (Map .of ("product_price" , 12.99 ));
73- or2Criteria .get ("$or" ).add (Map .of ("product_name" , "HealthyFresh - Beef raw dog food" ));
74- List <Map <String , List <Map <String , Object >>>> andCriteria = new ArrayList <>();
75- andCriteria .add (or1Criteria );
76- andCriteria .add (or2Criteria );
77- sq2 .getFilter ().put ("$and" , andCriteria );
78- collection .findOne (sq2 ).ifPresent (System .out ::println );
79-
80- // Complex query with AND and OR as String
81- collection .findOne ("{\" filter\" :{" +
82- "\" $and\" :[" +
76+ // Complex query with AND and OR as String
77+ collection .findOne ("{\" filter\" :{" +
78+ "\" $and\" :[" +
8379 "{\" $or\" :[" +
8480 " {\" product_price\" :9.99}," +
8581 " {\" product_name\" :\" HealthyFresh - Beef raw dog food\" }" +
@@ -90,8 +86,9 @@ public static void main(String[] args) {
9086 " {\" product_name\" :\" HealthyFresh - Beef raw dog food\" }" +
9187 " ]" +
9288 "}" +
93- "]" +
94- "}}" ).ifPresent (System .out ::println );
95- }
89+ "]" +
90+ "}}" )
91+ .ifPresent (System .out ::println );
92+ }
9693
9794}
0 commit comments