Skip to content

Commit 1f30eaa

Browse files
committed
improve IndexOptions
1 parent c24f77f commit 1f30eaa

File tree

1 file changed

+82
-105
lines changed

1 file changed

+82
-105
lines changed

server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

Lines changed: 82 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,17 @@ public void validateDimension(int dim) {
12361236
throw new IllegalArgumentException(type.name + " only supports even dimensions; provided=" + dim);
12371237
}
12381238

1239+
@Override
1240+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1241+
builder.startObject();
1242+
builder.field("type", type);
1243+
innerXContent(builder, params);
1244+
builder.endObject();
1245+
return builder;
1246+
}
1247+
1248+
abstract public XContentBuilder innerXContent(XContentBuilder builder, Params params) throws IOException;
1249+
12391250
abstract boolean doEquals(IndexOptions other);
12401251

12411252
abstract int doHashCode();
@@ -1258,6 +1269,53 @@ public final int hashCode() {
12581269
}
12591270
}
12601271

1272+
abstract static class AbstractHnswIndexOptions extends IndexOptions {
1273+
protected final int m;
1274+
protected final int efConstruction;
1275+
1276+
AbstractHnswIndexOptions(VectorIndexType type, int m, int efConstruction) {
1277+
super(type);
1278+
this.m = m;
1279+
this.efConstruction = efConstruction;
1280+
}
1281+
1282+
public int getM() {
1283+
return m;
1284+
}
1285+
1286+
public int getEfConstruction() {
1287+
return efConstruction;
1288+
}
1289+
1290+
@Override
1291+
public XContentBuilder innerXContent(XContentBuilder builder, Params params) throws IOException {
1292+
builder.field("m", m);
1293+
builder.field("ef_construction", efConstruction);
1294+
innerHnswXContent(builder, params);
1295+
return builder;
1296+
}
1297+
1298+
abstract public XContentBuilder innerHnswXContent(XContentBuilder builder, Params params) throws IOException;
1299+
1300+
@Override
1301+
public boolean doEquals(IndexOptions o) {
1302+
if (this == o) return true;
1303+
if (o == null || getClass() != o.getClass()) return false;
1304+
HnswIndexOptions that = (HnswIndexOptions) o;
1305+
return m == that.m && efConstruction == that.efConstruction;
1306+
}
1307+
1308+
@Override
1309+
public int doHashCode() {
1310+
return Objects.hash(m, efConstruction);
1311+
}
1312+
1313+
@Override
1314+
public String toString() {
1315+
return "{type=" + type + ", m=" + m + ", ef_construction=" + efConstruction + "}";
1316+
}
1317+
}
1318+
12611319
public enum VectorIndexType {
12621320
HNSW("hnsw", false) {
12631321
@Override
@@ -1492,13 +1550,10 @@ static class Int8FlatIndexOptions extends IndexOptions {
14921550
}
14931551

14941552
@Override
1495-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1496-
builder.startObject();
1497-
builder.field("type", type);
1553+
public XContentBuilder innerXContent(XContentBuilder builder, Params params) throws IOException {
14981554
if (confidenceInterval != null) {
14991555
builder.field("confidence_interval", confidenceInterval);
15001556
}
1501-
builder.endObject();
15021557
return builder;
15031558
}
15041559

@@ -1536,10 +1591,7 @@ static class FlatIndexOptions extends IndexOptions {
15361591
}
15371592

15381593
@Override
1539-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1540-
builder.startObject();
1541-
builder.field("type", type);
1542-
builder.endObject();
1594+
public XContentBuilder innerXContent(XContentBuilder builder, Params params) throws IOException {
15431595
return builder;
15441596
}
15451597

@@ -1567,15 +1619,11 @@ public int doHashCode() {
15671619
}
15681620
}
15691621

1570-
static class Int4HnswIndexOptions extends IndexOptions {
1571-
private final int m;
1572-
private final int efConstruction;
1622+
static class Int4HnswIndexOptions extends AbstractHnswIndexOptions {
15731623
private final float confidenceInterval;
15741624

15751625
Int4HnswIndexOptions(int m, int efConstruction, Float confidenceInterval) {
1576-
super(VectorIndexType.INT4_HNSW);
1577-
this.m = m;
1578-
this.efConstruction = efConstruction;
1626+
super(VectorIndexType.INT4_HNSW, m, efConstruction);
15791627
// The default confidence interval for int4 is dynamic quantiles, this provides the best relevancy and is
15801628
// effectively required for int4 to behave well across a wide range of data.
15811629
this.confidenceInterval = confidenceInterval == null ? 0f : confidenceInterval;
@@ -1588,25 +1636,19 @@ public KnnVectorsFormat getVectorsFormat(ElementType elementType) {
15881636
}
15891637

15901638
@Override
1591-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1592-
builder.startObject();
1593-
builder.field("type", type);
1594-
builder.field("m", m);
1595-
builder.field("ef_construction", efConstruction);
1639+
public XContentBuilder innerHnswXContent(XContentBuilder builder, Params params) throws IOException {
15961640
builder.field("confidence_interval", confidenceInterval);
1597-
builder.endObject();
15981641
return builder;
15991642
}
16001643

16011644
@Override
16021645
public boolean doEquals(IndexOptions o) {
1603-
Int4HnswIndexOptions that = (Int4HnswIndexOptions) o;
1604-
return m == that.m && efConstruction == that.efConstruction && Objects.equals(confidenceInterval, that.confidenceInterval);
1646+
return super.doEquals(o) && Objects.equals(confidenceInterval, ((Int4HnswIndexOptions) o).confidenceInterval);
16051647
}
16061648

16071649
@Override
16081650
public int doHashCode() {
1609-
return Objects.hash(m, efConstruction, confidenceInterval);
1651+
return super.doHashCode() + Objects.hash(confidenceInterval);
16101652
}
16111653

16121654
@Override
@@ -1652,11 +1694,8 @@ public KnnVectorsFormat getVectorsFormat(ElementType elementType) {
16521694
}
16531695

16541696
@Override
1655-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1656-
builder.startObject();
1657-
builder.field("type", type);
1697+
public XContentBuilder innerXContent(XContentBuilder builder, Params params) throws IOException {
16581698
builder.field("confidence_interval", confidenceInterval);
1659-
builder.endObject();
16601699
return builder;
16611700
}
16621701

@@ -1689,15 +1728,11 @@ boolean updatableTo(IndexOptions update) {
16891728

16901729
}
16911730

1692-
static class Int8HnswIndexOptions extends IndexOptions {
1693-
private final int m;
1694-
private final int efConstruction;
1731+
static class Int8HnswIndexOptions extends AbstractHnswIndexOptions {
16951732
private final Float confidenceInterval;
16961733

16971734
Int8HnswIndexOptions(int m, int efConstruction, Float confidenceInterval) {
1698-
super(VectorIndexType.INT8_HNSW);
1699-
this.m = m;
1700-
this.efConstruction = efConstruction;
1735+
super(VectorIndexType.INT8_HNSW, m, efConstruction);
17011736
this.confidenceInterval = confidenceInterval;
17021737
}
17031738

@@ -1708,29 +1743,21 @@ public KnnVectorsFormat getVectorsFormat(ElementType elementType) {
17081743
}
17091744

17101745
@Override
1711-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1712-
builder.startObject();
1713-
builder.field("type", type);
1714-
builder.field("m", m);
1715-
builder.field("ef_construction", efConstruction);
1746+
public XContentBuilder innerHnswXContent(XContentBuilder builder, Params params) throws IOException {
17161747
if (confidenceInterval != null) {
17171748
builder.field("confidence_interval", confidenceInterval);
17181749
}
1719-
builder.endObject();
17201750
return builder;
17211751
}
17221752

17231753
@Override
17241754
public boolean doEquals(IndexOptions o) {
1725-
if (this == o) return true;
1726-
if (o == null || getClass() != o.getClass()) return false;
1727-
Int8HnswIndexOptions that = (Int8HnswIndexOptions) o;
1728-
return m == that.m && efConstruction == that.efConstruction && Objects.equals(confidenceInterval, that.confidenceInterval);
1755+
return super.doEquals(o) && Objects.equals(confidenceInterval, ((Int8HnswIndexOptions) o).confidenceInterval);
17291756
}
17301757

17311758
@Override
17321759
public int doHashCode() {
1733-
return Objects.hash(m, efConstruction, confidenceInterval);
1760+
return super.doHashCode() + Objects.hash(confidenceInterval);
17341761
}
17351762

17361763
@Override
@@ -1764,14 +1791,10 @@ boolean updatableTo(IndexOptions update) {
17641791
}
17651792
}
17661793

1767-
static class HnswIndexOptions extends IndexOptions {
1768-
private final int m;
1769-
private final int efConstruction;
1794+
static class HnswIndexOptions extends AbstractHnswIndexOptions {
17701795

17711796
HnswIndexOptions(int m, int efConstruction) {
1772-
super(VectorIndexType.HNSW);
1773-
this.m = m;
1774-
this.efConstruction = efConstruction;
1797+
super(VectorIndexType.HNSW, m, efConstruction);
17751798
}
17761799

17771800
@Override
@@ -1796,42 +1819,15 @@ boolean updatableTo(IndexOptions update) {
17961819
}
17971820

17981821
@Override
1799-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1800-
builder.startObject();
1801-
builder.field("type", type);
1802-
builder.field("m", m);
1803-
builder.field("ef_construction", efConstruction);
1804-
builder.endObject();
1822+
public XContentBuilder innerHnswXContent(XContentBuilder builder, Params params) throws IOException {
18051823
return builder;
18061824
}
1807-
1808-
@Override
1809-
public boolean doEquals(IndexOptions o) {
1810-
if (this == o) return true;
1811-
if (o == null || getClass() != o.getClass()) return false;
1812-
HnswIndexOptions that = (HnswIndexOptions) o;
1813-
return m == that.m && efConstruction == that.efConstruction;
1814-
}
1815-
1816-
@Override
1817-
public int doHashCode() {
1818-
return Objects.hash(m, efConstruction);
1819-
}
1820-
1821-
@Override
1822-
public String toString() {
1823-
return "{type=" + type + ", m=" + m + ", ef_construction=" + efConstruction + "}";
1824-
}
18251825
}
18261826

1827-
static class BBQHnswIndexOptions extends IndexOptions {
1828-
private final int m;
1829-
private final int efConstruction;
1827+
static class BBQHnswIndexOptions extends AbstractHnswIndexOptions {
18301828

18311829
BBQHnswIndexOptions(int m, int efConstruction) {
1832-
super(VectorIndexType.BBQ_HNSW);
1833-
this.m = m;
1834-
this.efConstruction = efConstruction;
1830+
super(VectorIndexType.BBQ_HNSW, m, efConstruction);
18351831
}
18361832

18371833
@Override
@@ -1845,34 +1841,18 @@ boolean updatableTo(IndexOptions update) {
18451841
return update.type.equals(this.type);
18461842
}
18471843

1848-
@Override
1849-
boolean doEquals(IndexOptions other) {
1850-
BBQHnswIndexOptions that = (BBQHnswIndexOptions) other;
1851-
return m == that.m && efConstruction == that.efConstruction;
1852-
}
1853-
1854-
@Override
1855-
int doHashCode() {
1856-
return Objects.hash(m, efConstruction);
1857-
}
1858-
1859-
@Override
1860-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1861-
builder.startObject();
1862-
builder.field("type", type);
1863-
builder.field("m", m);
1864-
builder.field("ef_construction", efConstruction);
1865-
builder.endObject();
1866-
return builder;
1867-
}
1868-
18691844
@Override
18701845
public void validateDimension(int dim) {
18711846
if (type.supportsDimension(dim)) {
18721847
return;
18731848
}
18741849
throw new IllegalArgumentException(type.name + " does not support dimensions fewer than " + BBQ_MIN_DIMS + "; provided=" + dim);
18751850
}
1851+
1852+
@Override
1853+
public XContentBuilder innerHnswXContent(XContentBuilder builder, Params params) throws IOException {
1854+
return builder;
1855+
}
18761856
}
18771857

18781858
static class BBQFlatIndexOptions extends IndexOptions {
@@ -1904,10 +1884,7 @@ int doHashCode() {
19041884
}
19051885

19061886
@Override
1907-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1908-
builder.startObject();
1909-
builder.field("type", type);
1910-
builder.endObject();
1887+
public XContentBuilder innerXContent(XContentBuilder builder, Params params) throws IOException {
19111888
return builder;
19121889
}
19131890

0 commit comments

Comments
 (0)