-
Notifications
You must be signed in to change notification settings - Fork 25.8k
semantic_text: Add exists query #110027
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
Merged
carlosdelest
merged 14 commits into
elastic:main
from
carlosdelest:carlosdelest/semantic-text-exists-query
Jul 1, 2024
Merged
semantic_text: Add exists query #110027
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0a5b53
Add exists query to semantic_text
carlosdelest 12b8533
Merge branch 'refs/heads/main' into carlosdelest/semantic-text-exists…
carlosdelest 8ca22c8
Fix exists query test
carlosdelest 63cc67b
Added tests for the actual implementation of the exists query
carlosdelest 9c7ba83
Use rewrite - bad idea
carlosdelest 3dc6c1d
Refactor NestedQueryBuilder to expose toQuery method
carlosdelest 384a5ad
Fix nested scope
carlosdelest 6825311
Small fixes
carlosdelest a5a3bbe
checkstyle fix
carlosdelest b8d7315
Use a Query provider to restore previous doToQuery() semantics
carlosdelest cabf436
Extra test checks
carlosdelest 8843eb7
Merge branch 'refs/heads/main' into carlosdelest/semantic-text-exists…
carlosdelest c6394b5
Fix merge with main
carlosdelest ff4c980
Remove extra test checks
carlosdelest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import org.apache.lucene.search.BooleanClause; | ||
| import org.apache.lucene.search.BooleanQuery; | ||
| import org.apache.lucene.search.IndexSearcher; | ||
| import org.apache.lucene.search.MatchNoDocsQuery; | ||
| import org.apache.lucene.search.Query; | ||
| import org.apache.lucene.search.TermQuery; | ||
| import org.apache.lucene.search.TopDocs; | ||
|
|
@@ -42,6 +43,7 @@ | |
| import org.elasticsearch.index.mapper.SourceToParse; | ||
| import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper; | ||
| import org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper; | ||
| import org.elasticsearch.index.query.SearchExecutionContext; | ||
| import org.elasticsearch.index.search.ESToParentBlockJoinQuery; | ||
| import org.elasticsearch.inference.Model; | ||
| import org.elasticsearch.inference.SimilarityMeasure; | ||
|
|
@@ -180,36 +182,10 @@ public void testDynamicUpdate() throws IOException { | |
| final String fieldName = "semantic"; | ||
| final String inferenceId = "test_service"; | ||
|
|
||
| MapperService mapperService = createMapperService(mapping(b -> {})); | ||
|
Member
Author
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. I extracted this as a method to enable updating the mapping to check the actual exists queries created |
||
| mapperService.merge( | ||
| "_doc", | ||
| new CompressedXContent( | ||
| Strings.toString(PutMappingRequest.simpleMapping(fieldName, "type=semantic_text,inference_id=" + inferenceId)) | ||
| ), | ||
| MapperService.MergeReason.MAPPING_UPDATE | ||
| ); | ||
|
|
||
| SemanticTextField semanticTextField = new SemanticTextField( | ||
| MapperService mapperService = mapperServiceForFieldWithModelSettings( | ||
| fieldName, | ||
| List.of(), | ||
| new SemanticTextField.InferenceResult( | ||
| inferenceId, | ||
| new SemanticTextField.ModelSettings(TaskType.SPARSE_EMBEDDING, null, null, null), | ||
| List.of() | ||
| ), | ||
| XContentType.JSON | ||
| ); | ||
| XContentBuilder builder = JsonXContent.contentBuilder().startObject(); | ||
| builder.field(semanticTextField.fieldName()); | ||
| builder.value(semanticTextField); | ||
| builder.endObject(); | ||
|
|
||
| SourceToParse sourceToParse = new SourceToParse("test", BytesReference.bytes(builder), XContentType.JSON); | ||
| ParsedDocument parsedDocument = mapperService.documentMapper().parse(sourceToParse); | ||
| mapperService.merge( | ||
| "_doc", | ||
| parsedDocument.dynamicMappingsUpdate().toCompressedXContent(), | ||
| MapperService.MergeReason.MAPPING_UPDATE | ||
| inferenceId, | ||
| new SemanticTextField.ModelSettings(TaskType.SPARSE_EMBEDDING, null, null, null) | ||
| ); | ||
| assertSemanticTextField(mapperService, fieldName, true); | ||
| } | ||
|
|
@@ -565,6 +541,51 @@ private MapperService mapperServiceForFieldWithModelSettings( | |
| return mapperService; | ||
| } | ||
|
|
||
| public void testExistsQuerySparseVector() throws IOException { | ||
| final String fieldName = "semantic"; | ||
| final String inferenceId = "test_service"; | ||
|
|
||
| MapperService mapperService = mapperServiceForFieldWithModelSettings( | ||
| fieldName, | ||
| inferenceId, | ||
| new SemanticTextField.ModelSettings(TaskType.SPARSE_EMBEDDING, null, null, null) | ||
| ); | ||
|
|
||
| Mapper mapper = mapperService.mappingLookup().getMapper(fieldName); | ||
| assertNotNull(mapper); | ||
| SearchExecutionContext searchExecutionContext = createSearchExecutionContext(mapperService); | ||
| Query existsQuery = ((SemanticTextFieldMapper) mapper).fieldType().existsQuery(searchExecutionContext); | ||
| assertThat(existsQuery, instanceOf(ESToParentBlockJoinQuery.class)); | ||
| } | ||
|
|
||
| public void testExistsQueryDenseVector() throws IOException { | ||
| final String fieldName = "semantic"; | ||
| final String inferenceId = "test_service"; | ||
|
|
||
| MapperService mapperService = mapperServiceForFieldWithModelSettings( | ||
| fieldName, | ||
| inferenceId, | ||
| new SemanticTextField.ModelSettings( | ||
| TaskType.TEXT_EMBEDDING, | ||
| 1024, | ||
| SimilarityMeasure.COSINE, | ||
| DenseVectorFieldMapper.ElementType.FLOAT | ||
| ) | ||
| ); | ||
|
|
||
| Mapper mapper = mapperService.mappingLookup().getMapper(fieldName); | ||
| assertNotNull(mapper); | ||
| SearchExecutionContext searchExecutionContext = createSearchExecutionContext(mapperService); | ||
| Query existsQuery = ((SemanticTextFieldMapper) mapper).fieldType().existsQuery(searchExecutionContext); | ||
| assertThat(existsQuery, instanceOf(ESToParentBlockJoinQuery.class)); | ||
| } | ||
|
|
||
| @Override | ||
| protected void assertExistsQuery(MappedFieldType fieldType, Query query, LuceneDocument fields) { | ||
Mikep86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Until a doc is indexed, the query is rewritten as match no docs | ||
| assertThat(query, instanceOf(MatchNoDocsQuery.class)); | ||
| } | ||
|
|
||
| private static void addSemanticTextMapping(XContentBuilder mappingBuilder, String fieldName, String modelId) throws IOException { | ||
| mappingBuilder.startObject(fieldName); | ||
| mappingBuilder.field("type", SemanticTextFieldMapper.CONTENT_TYPE); | ||
|
|
||
144 changes: 144 additions & 0 deletions
144
...src/yamlRestTest/resources/rest-api-spec/test/inference/70_semantic_text_exists_query.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| setup: | ||
| - requires: | ||
| cluster_features: "gte_v8.15.0" | ||
| reason: semantic_text introduced in 8.15.0 | ||
|
|
||
| - do: | ||
| inference.put: | ||
| task_type: sparse_embedding | ||
| inference_id: sparse-inference-id | ||
| body: > | ||
| { | ||
| "service": "test_service", | ||
| "service_settings": { | ||
| "model": "my_model", | ||
| "api_key": "abc64" | ||
| }, | ||
| "task_settings": { | ||
| } | ||
| } | ||
|
|
||
| - do: | ||
| inference.put: | ||
| task_type: text_embedding | ||
| inference_id: dense-inference-id | ||
| body: > | ||
| { | ||
| "service": "text_embedding_test_service", | ||
| "service_settings": { | ||
| "model": "my_model", | ||
| "dimensions": 10, | ||
| "api_key": "abc64", | ||
| "similarity": "COSINE" | ||
| }, | ||
| "task_settings": { | ||
| } | ||
| } | ||
|
|
||
| - do: | ||
| indices.create: | ||
| index: test-sparse-index | ||
| body: | ||
| mappings: | ||
| properties: | ||
| inference_field: | ||
| type: semantic_text | ||
| inference_id: sparse-inference-id | ||
|
|
||
| - do: | ||
| indices.create: | ||
| index: test-dense-index | ||
| body: | ||
| mappings: | ||
| properties: | ||
| inference_field: | ||
| type: semantic_text | ||
| inference_id: dense-inference-id | ||
|
|
||
| --- | ||
| "Exists query with no indexed documents": | ||
| - do: | ||
| search: | ||
| index: test-sparse-index | ||
| body: | ||
| query: | ||
| exists: | ||
| field: "inference_field" | ||
|
|
||
| - match: { hits.total.value: 0 } | ||
|
|
||
| --- | ||
| "Exists query with null indexed documents": | ||
| - do: | ||
| index: | ||
| index: test-sparse-index | ||
| id: doc | ||
| body: | ||
| inference_field: null | ||
| refresh: true | ||
|
|
||
| - do: | ||
| search: | ||
| index: test-sparse-index | ||
| body: | ||
| query: | ||
| exists: | ||
| field: "inference_field" | ||
|
|
||
| - match: { hits.total.value: 0 } | ||
|
|
||
| - do: | ||
| index: | ||
| index: test-dense-index | ||
| id: doc | ||
| body: | ||
| inference_field: null | ||
| refresh: true | ||
|
|
||
| - do: | ||
| search: | ||
| index: test-dense-index | ||
| body: | ||
| query: | ||
| exists: | ||
| field: "inference_field" | ||
|
|
||
| - match: { hits.total.value: 0 } | ||
|
|
||
| --- | ||
| "Exists query with indexed documents": | ||
| - do: | ||
| index: | ||
| index: test-sparse-index | ||
| id: doc | ||
| body: | ||
| inference_field: "hello world" | ||
| refresh: true | ||
|
|
||
| - do: | ||
| search: | ||
| index: test-sparse-index | ||
| body: | ||
| query: | ||
| exists: | ||
| field: "inference_field" | ||
|
|
||
| - match: { hits.total.value: 1 } | ||
|
|
||
| - do: | ||
| index: | ||
| index: test-dense-index | ||
| id: doc | ||
| body: | ||
| inference_field: "hello world" | ||
| refresh: true | ||
|
|
||
| - do: | ||
| search: | ||
| index: test-dense-index | ||
| body: | ||
| query: | ||
| exists: | ||
| field: "inference_field" | ||
|
|
||
| - match: { hits.total.value: 1 } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Great
CheckedFunctionusage!