-
Notifications
You must be signed in to change notification settings - Fork 2k
Add support for Elasticsearch vector store: Squashed #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,6 @@ | |
<module>models/spring-ai-mistral-ai</module> | ||
<module>models/spring-ai-vertex-ai-palm2</module> | ||
<module>models/spring-ai-vertex-ai-gemini</module> | ||
<module>models/spring-ai-anthropic</module> | ||
|
||
<module>spring-ai-test</module> | ||
<module>spring-ai-spring-boot-autoconfigure</module> | ||
<module>spring-ai-spring-boot-starters/spring-ai-starter-openai</module> | ||
|
@@ -56,17 +54,13 @@ | |
<module>vector-stores/spring-ai-azure</module> | ||
<module>vector-stores/spring-ai-weaviate</module> | ||
<module>vector-stores/spring-ai-redis</module> | ||
<module>vector-stores/spring-ai-gemfire</module> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid module. should not be deleted. |
||
<module>spring-ai-spring-boot-starters/spring-ai-starter-vertex-ai-palm2</module> | ||
<module>spring-ai-spring-boot-starters/spring-ai-starter-vertex-ai-gemini</module> | ||
<module>vector-stores/spring-ai-qdrant</module> | ||
<module>spring-ai-spring-boot-starters/spring-ai-starter-bedrock-ai</module> | ||
<module>spring-ai-spring-boot-starters/spring-ai-starter-mistral-ai</module> | ||
<module>spring-ai-retry</module> | ||
<module>vector-stores/spring-ai-mongodb-atlas-store</module> | ||
<module>spring-ai-spring-boot-starters/spring-ai-starter-mongodb-atlas-store</module> | ||
<module>spring-ai-spring-boot-testcontainers</module> | ||
<module>spring-ai-spring-boot-starters/spring-ai-starter-anthropic</module> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All
Are valid and used modules. Should not be deleted. |
||
<module>vector-stores/spring-ai-elasticsearch-store</module> | ||
</modules> | ||
|
||
<organization> | ||
|
@@ -117,7 +111,7 @@ | |
<azure-open-ai-client.version>1.0.0-beta.7</azure-open-ai-client.version> | ||
<jtokkit.version>1.0.0</jtokkit.version> | ||
<victools.version>4.31.1</victools.version> | ||
<bedrockruntime.version>2.25.3</bedrockruntime.version> | ||
<bedrockruntime.version>2.24.8</bedrockruntime.version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the Bedrock runtime is downgrated? It is not related to ES |
||
<jackson.version>2.16.1</jackson.version> | ||
<djl.version>0.26.0</djl.version> | ||
<onnxruntime.version>1.17.0</onnxruntime.version> | ||
|
@@ -140,7 +134,7 @@ | |
<qdrant.version>1.7.1</qdrant.version> | ||
|
||
<!-- testing dependencies --> | ||
<testcontainers.version>1.19.7</testcontainers.version> | ||
<testcontainers.version>1.19.6</testcontainers.version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. This version shouldn't be downgraded |
||
|
||
<!-- documentation dependencies --> | ||
<io.spring.maven.antora-version>0.0.4</io.spring.maven.antora-version> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,7 +182,7 @@ | |
|
||
<dependency> | ||
<groupId>org.springframework.ai</groupId> | ||
<artifactId>spring-ai-mongodb-atlas-store</artifactId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the spring-ai-mongodb-atlas-store is used. Why is it deleted? |
||
<artifactId>spring-ai-elasticsearch-store</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.ai</groupId> | ||
<artifactId>spring-ai</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>spring-ai-elasticsearch-store</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Spring AI Vector Store - Elasticsearch</name> | ||
<description>Spring AI Elasticsearch Vector Store</description> | ||
<url>https://github.com/spring-projects/spring-ai</url> | ||
|
||
<scm> | ||
<url>https://github.com/spring-projects/spring-ai</url> | ||
<connection>git://github.com/spring-projects/spring-ai.git</connection> | ||
<developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection> | ||
</scm> | ||
|
||
<properties> | ||
<!-- testing --> | ||
<hikari-cp.version>4.0.3</hikari-cp.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.ai</groupId> | ||
<artifactId>spring-ai-core</artifactId> | ||
<version>${parent.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>co.elastic.clients</groupId> | ||
<artifactId>elasticsearch-java</artifactId> | ||
<version>8.12.2</version> | ||
</dependency> | ||
|
||
<!-- TESTING --> | ||
<dependency> | ||
<groupId>org.springframework.ai</groupId> | ||
<artifactId>spring-ai-openai</artifactId> | ||
<version>${parent.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>org.springframework.ai</groupId> | ||
<artifactId>spring-ai-test</artifactId> | ||
<version>${parent.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>elasticsearch</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>${testcontainers.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.vectorstore; | ||
|
||
import org.springframework.ai.vectorstore.filter.Filter; | ||
import org.springframework.ai.vectorstore.filter.Filter.Expression; | ||
import org.springframework.ai.vectorstore.filter.Filter.Key; | ||
import org.springframework.ai.vectorstore.filter.converter.AbstractFilterExpressionConverter; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.TimeZone; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author Jemin Huh | ||
* @since 1.0.0 | ||
*/ | ||
public class ElasticsearchAiSearchFilterExpressionConverter extends AbstractFilterExpressionConverter { | ||
|
||
private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"); | ||
|
||
private final SimpleDateFormat dateFormat; | ||
|
||
public ElasticsearchAiSearchFilterExpressionConverter() { | ||
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | ||
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
} | ||
|
||
@Override | ||
protected void doExpression(Expression expression, StringBuilder context) { | ||
if (expression.type() == Filter.ExpressionType.IN || expression.type() == Filter.ExpressionType.NIN) { | ||
context.append(getOperationSymbol(expression)); | ||
context.append("("); | ||
this.convertOperand(expression.left(), context); | ||
this.convertOperand(expression.right(), context); | ||
context.append(")"); | ||
} | ||
else { | ||
this.convertOperand(expression.left(), context); | ||
context.append(getOperationSymbol(expression)); | ||
this.convertOperand(expression.right(), context); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doStartValueRange(Filter.Value listValue, StringBuilder context) { | ||
} | ||
|
||
@Override | ||
protected void doEndValueRange(Filter.Value listValue, StringBuilder context) { | ||
} | ||
|
||
@Override | ||
protected void doAddValueRangeSpitter(Filter.Value listValue, StringBuilder context) { | ||
context.append(" OR "); | ||
} | ||
|
||
private String getOperationSymbol(Expression exp) { | ||
return switch (exp.type()) { | ||
case AND -> " AND "; | ||
case OR -> " OR "; | ||
case EQ, IN -> ""; | ||
case NE -> " NOT "; | ||
case LT -> "<"; | ||
case LTE -> "<="; | ||
case GT -> ">"; | ||
case GTE -> ">="; | ||
case NIN -> "NOT "; | ||
default -> throw new RuntimeException("Not supported expression type: " + exp.type()); | ||
}; | ||
} | ||
|
||
@Override | ||
public void doKey(Key key, StringBuilder context) { | ||
var identifier = hasOuterQuotes(key.key()) ? removeOuterQuotes(key.key()) : key.key(); | ||
var prefixedIdentifier = withMetaPrefix(identifier); | ||
context.append(prefixedIdentifier.trim()).append(":"); | ||
} | ||
|
||
public String withMetaPrefix(String identifier) { | ||
return "metadata." + identifier; | ||
} | ||
|
||
@Override | ||
protected void doValue(Filter.Value filterValue, StringBuilder context) { | ||
if (filterValue.value() instanceof List list) { | ||
int c = 0; | ||
for (Object v : list) { | ||
context.append(v); | ||
if (c++ < list.size() - 1) { | ||
this.doAddValueRangeSpitter(filterValue, context); | ||
} | ||
} | ||
} | ||
else { | ||
this.doSingleValue(filterValue.value(), context); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doSingleValue(Object value, StringBuilder context) { | ||
if (value instanceof Date date) { | ||
context.append(this.dateFormat.format(date)); | ||
} | ||
else if (value instanceof String text) { | ||
if (DATE_FORMAT_PATTERN.matcher(text).matches()) { | ||
try { | ||
Date date = this.dateFormat.parse(text); | ||
context.append(this.dateFormat.format(date)); | ||
} | ||
catch (ParseException e) { | ||
throw new IllegalArgumentException("Invalid date type:" + text, e); | ||
} | ||
} | ||
else { | ||
context.append(text); | ||
} | ||
} | ||
else { | ||
context.append(value); | ||
} | ||
} | ||
|
||
@Override | ||
public void doStartGroup(Filter.Group group, StringBuilder context) { | ||
context.append("("); | ||
} | ||
|
||
@Override | ||
public void doEndGroup(Filter.Group group, StringBuilder context) { | ||
context.append(")"); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a valid module. Why did you delete it?