Skip to content

Commit 2110df0

Browse files
Updated the SparseVectorStrategy class to use sparse_vector query (#2657)
1 parent 8a27080 commit 2110df0

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

elasticsearch/helpers/vectorstore/_async/strategies.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def needs_inference(self) -> bool:
9696

9797

9898
class AsyncSparseVectorStrategy(AsyncRetrievalStrategy):
99-
"""Sparse retrieval strategy using the `text_expansion` processor."""
99+
"""Sparse retrieval strategy using the `sparse_vector` processor."""
100100

101101
def __init__(self, model_id: str = ".elser_model_2"):
102102
self.model_id = model_id
@@ -127,11 +127,10 @@ def es_query(
127127
"bool": {
128128
"must": [
129129
{
130-
"text_expansion": {
131-
f"{vector_field}.{self._tokens_field}": {
132-
"model_id": self.model_id,
133-
"model_text": query,
134-
}
130+
"sparse_vector": {
131+
"field": f"{vector_field}.{self._tokens_field}",
132+
"inference_id": self.model_id,
133+
"query": query,
135134
}
136135
}
137136
],
@@ -150,7 +149,7 @@ def es_mappings_settings(
150149
mappings: Dict[str, Any] = {
151150
"properties": {
152151
vector_field: {
153-
"properties": {self._tokens_field: {"type": "rank_features"}}
152+
"properties": {self._tokens_field: {"type": "sparse_vector"}}
154153
}
155154
}
156155
}
@@ -172,11 +171,12 @@ async def before_index_creation(
172171
{
173172
"inference": {
174173
"model_id": self.model_id,
175-
"target_field": vector_field,
176-
"field_map": {text_field: "text_field"},
177-
"inference_config": {
178-
"text_expansion": {"results_field": self._tokens_field}
179-
},
174+
"input_output": [
175+
{
176+
"input_field": text_field,
177+
"output_field": f"{vector_field}.{self._tokens_field}",
178+
},
179+
],
180180
}
181181
}
182182
],

elasticsearch/helpers/vectorstore/_sync/strategies.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def needs_inference(self) -> bool:
9696

9797

9898
class SparseVectorStrategy(RetrievalStrategy):
99-
"""Sparse retrieval strategy using the `text_expansion` processor."""
99+
"""Sparse retrieval strategy using the `sparse_vector` processor."""
100100

101101
def __init__(self, model_id: str = ".elser_model_2"):
102102
self.model_id = model_id
@@ -127,11 +127,10 @@ def es_query(
127127
"bool": {
128128
"must": [
129129
{
130-
"text_expansion": {
131-
f"{vector_field}.{self._tokens_field}": {
132-
"model_id": self.model_id,
133-
"model_text": query,
134-
}
130+
"sparse_vector": {
131+
"field": f"{vector_field}.{self._tokens_field}",
132+
"inference_id": self.model_id,
133+
"query": query,
135134
}
136135
}
137136
],
@@ -150,7 +149,7 @@ def es_mappings_settings(
150149
mappings: Dict[str, Any] = {
151150
"properties": {
152151
vector_field: {
153-
"properties": {self._tokens_field: {"type": "rank_features"}}
152+
"properties": {self._tokens_field: {"type": "sparse_vector"}}
154153
}
155154
}
156155
}
@@ -172,11 +171,12 @@ def before_index_creation(
172171
{
173172
"inference": {
174173
"model_id": self.model_id,
175-
"target_field": vector_field,
176-
"field_map": {text_field: "text_field"},
177-
"inference_config": {
178-
"text_expansion": {"results_field": self._tokens_field}
179-
},
174+
"input_output": [
175+
{
176+
"input_field": text_field,
177+
"output_field": f"{vector_field}.{self._tokens_field}",
178+
},
179+
],
180180
}
181181
}
182182
],

0 commit comments

Comments
 (0)