|
24 | 24 | import lombok.Getter;
|
25 | 25 |
|
26 | 26 | import java.util.LinkedHashMap;
|
| 27 | +import java.util.List; |
27 | 28 | import java.util.Map;
|
28 | 29 |
|
29 | 30 | /**
|
|
32 | 33 | @Getter
|
33 | 34 | public class FindOneOptions {
|
34 | 35 |
|
35 |
| - /** |
36 |
| - * Default constructor. |
37 |
| - */ |
38 |
| - public FindOneOptions() { |
39 |
| - } |
40 |
| - |
41 | 36 | /**
|
42 | 37 | * Order by.
|
43 | 38 | */
|
44 |
| - private Document sort; |
| 39 | + private final Document sort; |
45 | 40 |
|
46 | 41 | /**
|
47 | 42 | * Select.
|
48 | 43 | */
|
49 |
| - private Map<String, Integer> projection; |
| 44 | + private final Map<String, Integer> projection; |
50 | 45 |
|
51 | 46 | /**
|
52 | 47 | * Options.
|
53 | 48 | */
|
54 |
| - private Boolean includeSimilarity; |
| 49 | + private final Boolean includeSimilarity; |
55 | 50 |
|
56 | 51 | /**
|
57 |
| - * Fluent api. |
| 52 | + * Create a builder for those options. |
58 | 53 | *
|
59 | 54 | * @return
|
60 |
| - * add a filter |
| 55 | + * instance of the builder. |
61 | 56 | */
|
62 |
| - public FindOneOptions includeSimilarity() { |
63 |
| - includeSimilarity = true; |
64 |
| - return this; |
| 57 | + public static FindOneOptionsBuilder builder() { |
| 58 | + return new FindOneOptionsBuilder(); |
65 | 59 | }
|
66 | 60 |
|
67 | 61 | /**
|
68 |
| - * Fluent api. |
69 |
| - * |
70 |
| - * @param pProjection |
71 |
| - * add a project field |
72 |
| - * @return |
73 |
| - * current command. |
| 62 | + * Default constructor. |
74 | 63 | */
|
75 |
| - public FindOneOptions projection(Map<String, Integer> pProjection) { |
76 |
| - Assert.notNull(pProjection, "projection"); |
77 |
| - if (this.projection == null) { |
78 |
| - this.projection = new LinkedHashMap<>(); |
79 |
| - } |
80 |
| - this.projection.putAll(pProjection); |
81 |
| - return this; |
| 64 | + public FindOneOptions(FindOneOptionsBuilder builder) { |
| 65 | + this.sort = builder.sort; |
| 66 | + this.projection = builder.projection; |
| 67 | + this.includeSimilarity = builder.includeSimilarity; |
82 | 68 | }
|
83 | 69 |
|
84 | 70 | /**
|
85 |
| - * Fluent api. |
86 |
| - * |
87 |
| - * @param pSort |
88 |
| - * add a filter |
89 |
| - * @return |
90 |
| - * current command. |
| 71 | + * Find is an operation with multiple options to filter, sort, project, skip, limit, and more. |
| 72 | + * This builder will help to chain options. |
91 | 73 | */
|
92 |
| - public FindOneOptions sort(Document pSort) { |
93 |
| - Assert.notNull(pSort, "projection"); |
94 |
| - if (this.sort == null) { |
95 |
| - sort = new Document(); |
| 74 | + public static class FindOneOptionsBuilder { |
| 75 | + |
| 76 | + /** |
| 77 | + * Order by. |
| 78 | + */ |
| 79 | + private Document sort; |
| 80 | + |
| 81 | + /** |
| 82 | + * Projection for return document (select) |
| 83 | + */ |
| 84 | + private Map<String, Integer> projection; |
| 85 | + |
| 86 | + /** |
| 87 | + * Flag to include similarity in the result when operating a semantic search. |
| 88 | + */ |
| 89 | + private Boolean includeSimilarity; |
| 90 | + |
| 91 | + /** |
| 92 | + * Default Builder. |
| 93 | + */ |
| 94 | + public FindOneOptionsBuilder() {} |
| 95 | + |
| 96 | + /** |
| 97 | + * Fluent api. |
| 98 | + * |
| 99 | + * @return |
| 100 | + * add a filter |
| 101 | + */ |
| 102 | + public FindOneOptionsBuilder withIncludeSimilarity() { |
| 103 | + this.includeSimilarity = true; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Provide a way to enter projections elements |
| 109 | + * |
| 110 | + * @param projections |
| 111 | + * projections as a list. |
| 112 | + * @return |
| 113 | + * self reference |
| 114 | + */ |
| 115 | + public FindOneOptionsBuilder withProjection(List<Projection> projections) { |
| 116 | + Assert.notNull(projections, "projection"); |
| 117 | + if (this.projection == null) { |
| 118 | + this.projection = new LinkedHashMap<>(); |
| 119 | + } |
| 120 | + for (Projection p : projections) { |
| 121 | + this.projection.put(p.getField(), p.isPresent() ? 1 : 0); |
| 122 | + } |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Fluent api. |
| 128 | + * |
| 129 | + * @param pProjection |
| 130 | + * add a project field |
| 131 | + * @return |
| 132 | + * current command. |
| 133 | + */ |
| 134 | + public FindOneOptionsBuilder withProjection(Map<String, Integer> pProjection) { |
| 135 | + Assert.notNull(pProjection, "projection"); |
| 136 | + if (this.projection == null) { |
| 137 | + this.projection = new LinkedHashMap<>(); |
| 138 | + } |
| 139 | + this.projection.putAll(pProjection); |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Add a criteria with $vectorize in the sort clause |
| 145 | + * |
| 146 | + * @param vectorize |
| 147 | + * an expression to look for vectorization |
| 148 | + * @return |
| 149 | + * current command |
| 150 | + */ |
| 151 | + public FindOneOptionsBuilder withVectorize(String vectorize) { |
| 152 | + Assert.hasLength(vectorize, "vectorize"); |
| 153 | + return sortBy(new Document().append(Document.VECTORIZE, vectorize)); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Add a criteria with $vector in the sort clause |
| 158 | + * |
| 159 | + * @param vector |
| 160 | + * vector float |
| 161 | + * @return |
| 162 | + * current command |
| 163 | + */ |
| 164 | + public FindOneOptionsBuilder withVector(float[] vector) { |
| 165 | + Assert.notNull(vector, "vector"); |
| 166 | + return sortBy(new Document().append(Document.VECTOR, vector)); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Fluent api. |
| 171 | + * |
| 172 | + * @param field |
| 173 | + * field name |
| 174 | + * @param order |
| 175 | + * orders |
| 176 | + * @return |
| 177 | + * Self reference |
| 178 | + */ |
| 179 | + public FindOneOptionsBuilder sortBy(String field, SortOrder order) { |
| 180 | + Assert.notNull(order, "order"); |
| 181 | + Assert.hasLength(field, "field"); |
| 182 | + if (this.sort == null) { |
| 183 | + sort = new Document(); |
| 184 | + } |
| 185 | + this.sort.put(field, order.getOrder()); |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Fluent api. |
| 191 | + * |
| 192 | + * @param sorts |
| 193 | + * list of sorts |
| 194 | + * @return |
| 195 | + * Self reference |
| 196 | + */ |
| 197 | + public FindOneOptionsBuilder sortBy(List<Sort> sorts) { |
| 198 | + Assert.notNull(sorts, "sort"); |
| 199 | + if (this.sort == null) { |
| 200 | + sort = new Document(); |
| 201 | + } |
| 202 | + for (Sort s : sorts) { |
| 203 | + this.sort.put(s.getField(), s.getSort().getOrder()); |
| 204 | + } |
| 205 | + return this; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Fluent api. |
| 210 | + * |
| 211 | + * @param pSort |
| 212 | + * add a filter |
| 213 | + * @return |
| 214 | + * current command. |
| 215 | + */ |
| 216 | + public FindOneOptionsBuilder sortBy(Document pSort) { |
| 217 | + Assert.notNull(pSort, "sort"); |
| 218 | + if (this.sort == null) { |
| 219 | + sort = new Document(); |
| 220 | + } |
| 221 | + this.sort.putAll(pSort); |
| 222 | + return this; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Builder for the find Options. |
| 227 | + * |
| 228 | + * @return |
| 229 | + * the find options object |
| 230 | + */ |
| 231 | + public FindOneOptions build() { |
| 232 | + return new FindOneOptions(this); |
96 | 233 | }
|
97 |
| - this.sort.putAll(pSort); |
98 |
| - return this; |
99 |
| - } |
100 | 234 |
|
101 |
| - /** |
102 |
| - * Add vector in the sort block. |
103 |
| - * |
104 |
| - * @param vector |
105 |
| - * vector float |
106 |
| - * @return |
107 |
| - * current command |
108 |
| - */ |
109 |
| - public FindOneOptions sortByVector(float[] vector) { |
110 |
| - return sort(new Document().append(Document.VECTOR, vector)); |
111 | 235 | }
|
112 | 236 |
|
113 | 237 | }
|
0 commit comments