TextSearcher

public final class TextSearcher

Performs similarity search on text string.

The API expects a TFLite model with optional, but strongly recommended, TFLite Model Metadata..

The API expects a TFLite model with metadata populated. The metadata should contain the following information:

  • For Bert based TFLite model:
    • 3 input tensors of type kTfLiteString with names "ids", "mask" and "segment_ids".
    • input_process_units for Wordpiece/Sentencepiece Tokenizer
    • exactly one output tensor of type kTfLiteFloat32
  • For Regex based TFLite model:
    • 1 input tensor.
    • input_process_units for RegexTokenizer Tokenizer
    • exactly one output tensor of type kTfLiteFloat32
  • For Universal Sentence Encoder based TFLite model:
    • 3 input tensors with names "inp_text", "res_context" and "res_text"
    • 2 output tensors with names "query_encoding" and "response_encoding" of type kTfLiteFloat32

TODO(b/180502532): add pointer to example model.

TODO(b/222671076): add factory create methods without options, such as `createFromFile`, once the single file format (index file packed in the model) is supported.

Nested Classes

class TextSearcher.TextSearcherOptions Options for setting up an TextSearcher. 

Public Methods

static TextSearcher
static TextSearcher
static TextSearcher
static TextSearcher
List<NearestNeighbor>
search(String text)
Performs embedding extraction on the provided string input, followed by nearest-neighbor search in the index.

Inherited Methods

Public Methods

public static TextSearcher createFromBufferAndOptions (ByteBuffer modelBuffer, TextSearcher.TextSearcherOptions options)

Creates an TextSearcher instance with a model buffer and TextSearcher.TextSearcherOptions.

Parameters
modelBuffer a direct ByteBuffer or a MappedByteBuffer of the search model
options
Throws
IllegalArgumentException if the model buffer is not a direct ByteBuffer or a MappedByteBuffer
IOException if an I/O error occurs when loading the index file
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error

public static TextSearcher createFromBufferAndOptionsImpl (ByteBuffer modelBuffer, TextSearcher.TextSearcherOptions options, int indexFd)

Parameters
modelBuffer
options
indexFd

public static TextSearcher createFromFileAndOptions (File modelFile, TextSearcher.TextSearcherOptions options)

Creates an TextSearcher instance.

Parameters
modelFile the search model File instance
options
Throws
IOException if an I/O error occurs when loading the tflite model or the index file
IllegalArgumentException if an argument is invalid
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error

public static TextSearcher createFromFileAndOptions (Context context, String modelPath, TextSearcher.TextSearcherOptions options)

Parameters
context
modelPath path of the search model with metadata in the assets
options
Throws
IOException if an I/O error occurs when loading the tflite model or the index file
IllegalArgumentException if an argument is invalid
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error

public List<NearestNeighbor> search (String text)

Performs embedding extraction on the provided string input, followed by nearest-neighbor search in the index.

Parameters
text input text query to the model