Java Client Library - ELSER

We are trying to leverage ELSER search functionalities within our Java application. we would like to know How to use Java client library for performing ELSER Search ?

RestHighLevelClient seems to be one solution we observed. But that is deprecated. Is there any examples or approaches to use ELSER search using Client library, similar to Searching for documents | Elasticsearch Java API Client [8.12] | Elastic

Elasticsearch java language-clients elastic-stack-machine-learning

What is the API you'd like to use?

If you want to use something like:

# Create Index
PUT my-index
{
  "mappings": {
    "properties": {
      "content_embedding": { 
        "type": "sparse_vector" 
      },
      "content": { 
        "type": "text" 
      }
    }
  }
}
# Create Pipeline
PUT _ingest/pipeline/elser-v2-test
{
  "processors": [
    {
      "inference": {
        "model_id": ".elser_model_2",
        "input_output": [ 
          {
            "input_field": "content",
            "output_field": "content_embedding"
          }
        ]
      }
    }
  ]
}
# Search
GET my-index/_search
{
   "query":{
      "text_expansion":{
         "content_embedding":{
            "model_id":".elser_model_2",
            "model_text":"How to avoid muscle soreness after running?"
         }
      }
   }
}

Then it would be something like (with 8.12.2 - not tested):

// Create the index
client.indices().create(cir -> cir.index(indexName).mappings(m -> m
        .properties("content", p -> p.text(tp -> tp))
        .properties("content_embedding", p -> p.sparseVector(sp -> sp))
));

// Create the pipeline
client.ingest().putPipeline(pr -> pr
        .id("elser-v2-test")
        .processors(p -> p
                .inference(i -> i
                        .modelId(".elser_model_2")
                        .fieldMap("content", JsonData.of("content"))
                        .targetField("content_embedding")
                )
        )
);

// Search
client.search(sr -> sr
        .index(indexName)
        .query(q -> q.textExpansion(te -> te
                .field("content_embedding")
                .modelId(".elser_model_2")
                .modelText("How to avoid muscle soreness after running?")
        )), ObjectNode.class);
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.