-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Update semantic_text field to support indexing numeric and boolean data types #111284
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 6 commits
1c9b03f
69fa91d
b3b3cf9
b65f4b3
168fdf7
08ff9dd
3692755
85f25b5
9b5bf98
a1651a7
44fa931
362bd62
1eaded4
74ee5eb
732691b
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.action.filter; | ||
|
|
||
| import static org.elasticsearch.test.ESTestCase.randomAlphaOfLengthBetween; | ||
| import static org.elasticsearch.test.ESTestCase.randomBoolean; | ||
| import static org.elasticsearch.test.ESTestCase.randomDouble; | ||
| import static org.elasticsearch.test.ESTestCase.randomFloat; | ||
| import static org.elasticsearch.test.ESTestCase.randomInt; | ||
| import static org.elasticsearch.test.ESTestCase.randomIntBetween; | ||
| import static org.elasticsearch.test.ESTestCase.randomLong; | ||
|
|
||
| public class ShardBulkInferenceActionFilterTestUtil { | ||
|
|
||
| /** | ||
| * Returns a randomly generated object for Semantic Text tests purpose. | ||
| */ | ||
| public static Object randomInputCasesForSemanticText() { | ||
| int randomInt = randomIntBetween(0, 4); | ||
| return switch (randomInt) { | ||
| case 0 -> randomAlphaOfLengthBetween(10, 20); | ||
| case 1 -> randomInt(); | ||
| case 2 -> randomLong(); | ||
| case 3 -> randomFloat(); | ||
| case 4 -> randomBoolean(); | ||
| default -> randomDouble(); | ||
Mikep86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| import org.elasticsearch.xcontent.json.JsonXContent; | ||
| import org.elasticsearch.xpack.core.inference.results.ErrorChunkedInferenceResults; | ||
| import org.elasticsearch.xpack.core.inference.results.InferenceChunkedSparseEmbeddingResults; | ||
| import org.elasticsearch.xpack.inference.mapper.SemanticTextField; | ||
| import org.elasticsearch.xpack.inference.model.TestModel; | ||
| import org.elasticsearch.xpack.inference.registry.ModelRegistry; | ||
| import org.junit.After; | ||
|
|
@@ -55,8 +56,10 @@ | |
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.awaitLatch; | ||
| import static org.elasticsearch.xpack.inference.action.filter.ShardBulkInferenceActionFilter.DEFAULT_BATCH_SIZE; | ||
| import static org.elasticsearch.xpack.inference.action.filter.ShardBulkInferenceActionFilter.getIndexRequestOrNull; | ||
| import static org.elasticsearch.xpack.inference.action.filter.ShardBulkInferenceActionFilterTestUtil.randomInputCasesForSemanticText; | ||
| import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText; | ||
| import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSparseEmbeddings; | ||
| import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.semanticTextFieldFromChunkedInferenceResults; | ||
| import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.toChunkedResult; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
@@ -331,16 +334,31 @@ private static BulkItemRequest[] randomBulkItemRequest( | |
| for (var entry : fieldInferenceMap.values()) { | ||
| String field = entry.getName(); | ||
| var model = modelMap.get(entry.getInferenceId()); | ||
| String text = randomAlphaOfLengthBetween(10, 20); | ||
| docMap.put(field, text); | ||
| expectedDocMap.put(field, text); | ||
| Object inputObject = randomInputCasesForSemanticText(); | ||
| String inputText = inputObject.toString(); | ||
| docMap.put(field, inputObject); | ||
| expectedDocMap.put(field, inputText); | ||
| if (model == null) { | ||
| // ignore results, the doc should fail with a resource not found exception | ||
| continue; | ||
| } | ||
| var result = randomSemanticText(field, model, List.of(text), requestContentType); | ||
| model.putResult(text, toChunkedResult(result)); | ||
| expectedDocMap.put(field, result); | ||
|
|
||
| SemanticTextField semanticTextField; | ||
| if (model.hasResult(inputText)) { | ||
| ChunkedInferenceServiceResults results = model.getResults(inputText); | ||
| semanticTextField = semanticTextFieldFromChunkedInferenceResults( | ||
| field, | ||
| model, | ||
| List.of(inputText), | ||
| results, | ||
| requestContentType | ||
| ); | ||
| } else { | ||
| semanticTextField = randomSemanticText(field, model, List.of(inputText), requestContentType); | ||
| model.putResult(inputText, toChunkedResult(semanticTextField)); | ||
| } | ||
|
Comment on lines
+350
to
+362
Contributor
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. @carlosdelest We had to make this change because the inference result cache in This updated logic checks if the inference result cache already has results for the value, and uses them if it does.
Member
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 see - we could maybe have used
Contributor
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. Maybe I'm misunderstanding your comment, but I don't think
This new logic fixes the problem by first checking if
Member
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. My point is, wouldn't it be simpler not to generate the duplicate input values, and thus avoid managing the results as it happens? |
||
|
|
||
| expectedDocMap.put(field, semanticTextField); | ||
| } | ||
|
|
||
| int requestId = randomIntBetween(0, Integer.MAX_VALUE); | ||
|
|
@@ -383,5 +401,9 @@ ChunkedInferenceServiceResults getResults(String text) { | |
| void putResult(String text, ChunkedInferenceServiceResults result) { | ||
| resultMap.put(text, result); | ||
| } | ||
|
|
||
| boolean hasResult(String text) { | ||
| return resultMap.containsKey(text); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,64 @@ setup: | |
| - close_to: { hits.hits.0._score: { value: 3.7837332e17, error: 1e10 } } | ||
| - length: { hits.hits.0._source.inference_field.inference.chunks: 2 } | ||
|
|
||
| --- | ||
| "Numeric query using a sparse embedding model": | ||
| - skip: | ||
| features: [ "headers", "close_to" ] | ||
|
|
||
| - do: | ||
| index: | ||
| index: test-sparse-index | ||
| id: doc_1 | ||
| body: | ||
| inference_field: [40, 49.678] | ||
| refresh: true | ||
|
|
||
| - do: | ||
| headers: | ||
| # Force JSON content type so that we use a parser that interprets the floating-point score as a double | ||
| Content-Type: application/json | ||
| search: | ||
| index: test-sparse-index | ||
| body: | ||
| query: | ||
| semantic: | ||
| field: "inference_field" | ||
| query: "40" | ||
|
|
||
| - match: { hits.total.value: 1 } | ||
| - match: { hits.hits.0._id: "doc_1" } | ||
| - length: { hits.hits.0._source.inference_field.inference.chunks: 2 } | ||
|
|
||
| --- | ||
| "Boolean query using a sparse embedding model": | ||
| - skip: | ||
| features: [ "headers", "close_to" ] | ||
|
|
||
| - do: | ||
| index: | ||
| index: test-sparse-index | ||
| id: doc_1 | ||
| body: | ||
| inference_field: true | ||
| refresh: true | ||
|
|
||
| - do: | ||
| headers: | ||
| # Force JSON content type so that we use a parser that interprets the floating-point score as a double | ||
|
||
| Content-Type: application/json | ||
| search: | ||
| index: test-sparse-index | ||
| body: | ||
| query: | ||
| semantic: | ||
| field: "inference_field" | ||
| query: "true" | ||
|
|
||
| - match: { hits.total.value: 1 } | ||
| - match: { hits.hits.0._id: "doc_1" } | ||
| - length: { hits.hits.0._source.inference_field.inference.chunks: 1 } | ||
|
|
||
| --- | ||
| "Query using a dense embedding model": | ||
| - skip: | ||
|
|
@@ -121,6 +179,64 @@ setup: | |
| - close_to: { hits.hits.0._score: { value: 1.0, error: 0.0001 } } | ||
| - length: { hits.hits.0._source.inference_field.inference.chunks: 2 } | ||
|
|
||
| --- | ||
| "Numeric query using a dense embedding model": | ||
| - skip: | ||
| features: [ "headers", "close_to" ] | ||
|
|
||
| - do: | ||
| index: | ||
| index: test-dense-index | ||
| id: doc_1 | ||
| body: | ||
| inference_field: [45.1, 100] | ||
| refresh: true | ||
|
|
||
| - do: | ||
| headers: | ||
| # Force JSON content type so that we use a parser that interprets the floating-point score as a double | ||
| Content-Type: application/json | ||
| search: | ||
| index: test-dense-index | ||
| body: | ||
| query: | ||
| semantic: | ||
| field: "inference_field" | ||
| query: "45.1" | ||
|
|
||
| - match: { hits.total.value: 1 } | ||
| - match: { hits.hits.0._id: "doc_1" } | ||
| - length: { hits.hits.0._source.inference_field.inference.chunks: 2 } | ||
|
|
||
| --- | ||
| "Boolean query using a dense embedding model": | ||
| - skip: | ||
| features: [ "headers", "close_to" ] | ||
|
|
||
| - do: | ||
| index: | ||
| index: test-dense-index | ||
| id: doc_1 | ||
| body: | ||
| inference_field: false | ||
| refresh: true | ||
|
|
||
| - do: | ||
| headers: | ||
| # Force JSON content type so that we use a parser that interprets the floating-point score as a double | ||
| Content-Type: application/json | ||
| search: | ||
| index: test-dense-index | ||
| body: | ||
| query: | ||
| semantic: | ||
| field: "inference_field" | ||
| query: "false" | ||
|
|
||
| - match: { hits.total.value: 1 } | ||
| - match: { hits.hits.0._id: "doc_1" } | ||
| - length: { hits.hits.0._source.inference_field.inference.chunks: 1 } | ||
|
|
||
| --- | ||
| "Query using a dense embedding model that uses byte embeddings": | ||
| - skip: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.