how to implement "suggest" query, with JavaClient in Elasticsearch - java

I want to execute the following query with JavaClient.
{
"suggest": {
"my-suggestion-1": {
"text": "sample",
"completion": {
"field": "suggest1",
"size": 10
}
}
}
}
I could not find the document.
Please let me know the URL of the document.
Also, how should I implement it?
Someone, please lend me your wisdom.
Environment
Language: Java 8
Framework: Spring
Elasticsearch client (jar) ver
Org.elasticsearch: 5.1.1
Org.elasticsearch.client: 5.1.1
Elasticsearch's ver: 5.3.1

Try something like
TermSuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("field_name").text("my suggest terms");
client.prepareSearch("my_index").setSize(0).suggest(new SuggestBuilder().addSuggestion("foo", termSuggestionBuilder)).get();
A good thing to do is usually to check the Elasticsearch tests itself, like the CompletionSuggestSearchIT

Related

How to perform GeoDistanceSort on a nested field using elasticsearch-8.5.3 JavaClient

We are using ElasticsearchClient instance to build our query and communicate with elasticsearch hosted on elastic-cloud version 8.5.3.
We could not find any information on how to inject nested path while creating GeoDistanceSort object.
Sort query that we are trying to build using Java:
"sort":[
{
"_geo_distance":{
"nested":{
"path":"facility"
},
"facility.location":{
"lat":25.240995,
"lon":55.751156
},
"unit":"km",
"order":"asc",
"mode" : "min",
"distance_type" : "arc",
"ignore_unmapped": true
}
}
]
Maven dependency:
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.5.3</version>
</dependency>
We have referred to the following official elastic documentation for JavaClient 8.5.3:
https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.5.3/co/elastic/clients/elasticsearch/_types/SortOptions.html
https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.5.3/co/elastic/clients/elasticsearch/_types/GeoDistanceSort.html
https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.5.3/co/elastic/clients/elasticsearch/_types/GeoDistanceSort.Builder.html

How to deploy java 17 lambda function through cloud formation

I'm planning to deploy java 17 function to AWS lambda. But since as per the documentation AWS didn't provide a bas image for java 17.
https://docs.aws.amazon.com/lambda/latest/dg/lambda-java.html
So I have a problem with what value should I use in cloudformation template Runtime field
"AMIIDLookup": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"LambdaExecutionRole",
"Arn"
]
},
"Code": {
"S3Bucket": "lambda-functions",
"S3Key": "amilookup.zip"
},
"Runtime": "Java11", # what is the alternative for this
"Timeout": 25,
"TracingConfig": {
"Mode": "Active"
}
}
}
There is no official Java17 runtime for Lambda yet, you would have to create a custom runtime on your own.
Robert is right, either create a custom runtime or use docker image to spin up your aws lambda function https://cloud.netapp.com/blog/aws-cvo-blg-aws-lambda-images-how-to-use-container-images-to-deploy-lambda

Create tables in local AWS dynamoDB besed on information from aws-sam template.yaml file using Java

I have running local dynamoDB in Java integration tests
AmazonDynamoDBLocal embeddedDynamo = DynamoDBEmbedded.create()
AmazonDynamoDB client = embeddedDynamo.amazonDynamoDB();
DynamoDB dynamoDB = new DynamoDB(client);
Now I need to create tables based on definition in SAM template.yaml file. I can just copy that file on classpath using Gradle and parse it by some Java/Groovy tools and then just use parsed map to feed the DynamoDB instance API to create those tables.
But I am wondering if there is any library or tool that does it for me?
Thank you,
Lukas
You will need to create it yourself. You can do this using a json file and the aws cli's dynamodb command, for example:
my-table.json
{
"TableName": "MyTable",
"KeySchema": [
{ "AttributeName": "Id", "KeyType": "HASH" }
],
"AttributeDefinitions": [
{ "AttributeName": "Id", "AttributeType": "S" }
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
aws cli's dynamodb create-table command example
aws dynamodb create-table --cli-input-json file://my-table.json --endpoint-url http://localhost:8000
Notes
you can do this without the json file.
more info in the docs here: https://docs.aws.amazon.com/cli/latest/reference/dynamodb/create-table.html

Elasticsearch 2.0 script_score from Java API

I've just upgraded to ES 2.0.0-rc1.
I use a local node for JUnit testing.
Settings settings = Settings.builder()
.put("script.inline", "on")
.put("script.indexed", "on")
.put("path.home", "/").build();
return NodeBuilder.nodeBuilder()
.settings(settings)
.local(true)
.clusterName("c").node();
My problem is that the upgraded version doesn't see my native scripts.
The query seems like this:
Script script = new Script("myscript", ScriptType.INDEXED, "native", params);
ScoreFunctionBuilder scoreBuilder = ScoreFunctionBuilders.scriptFunction(script);
The output is the following:
...
"functions" : [ {
"script_score" : {
"script" : {
"id" : "myscript",
"lang" : "native",
"params" : {
"searchMode" : "A"
}
}
}
...
This script Plugin is in the Maven dependency list.
It worked well with the former version however with this new version I get the following exception:
Caused by: org.elasticsearch.index.query.QueryParsingException: script_score the script could not be loaded
Caused by: org.elasticsearch.index.IndexNotFoundException: no such index
So how could I install the plugin to a local node?
Edit 1:
https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugin-authors.html / Loading plugins from the classpath
might be the solution. Nope.
Edit 2:
It seems that the ScoreFunctionBuilder has been changed.
1.7:
ScoreFunctionBuilder scoreBuilder = ScoreFunctionBuilders.scriptFunction("myscript", "native", params);
2.0:
Script script = new Script("myscript", ScriptType.INDEXED, "native", params);
ScoreFunctionBuilder scoreBuilder = ScoreFunctionBuilders.scriptFunction(script);
However this doesn't fits to native scripts.
I don't know why since it doesn't follow any logic but all you need to do is to use ScriptType.INLINE
Script script = new Script("myscript", ScriptType.INLINE, "native", params);
We can't use INDEXED because elasticsearch will look for an indexed script in its system and since our script isn't indexed per say... it wouldn't work.

How to index an array of element in Elasticsearch?

I'm using Elasticsearch 1.4.3 and I'm trying to create an automated "filler" for the database.
The idea is to use this website http://beta.json-generator.com/BhxCdZ6 to generate a random set of data and push it in an index of Elasticsearch.
For interfacing with Elasticsearch, I am using Elasticsearch for Java API mixed with the Elasticsearch web API.
I managed to push one user per time simply copy-pasting the information excluding the [ and ] characters and creating a shell script that calls
curl -XPOST 'http://localhost:9200/myindex/users/' -d '{
"name": {
"first": "Dickerson",
"last": "Wood"
}, etc...
If I try to copy a full block composed of 3 people and try to push the data with the same script
curl -XPOST 'http://localhost:9200/geocon/users/' -d '[
{
"name": {
"first": "Dickerson",
"last": "Wood"
}, etc ...
]
}'
The error returned is :
org.elasticsearch.index.mapper.MapperParsingException: Malformed content, must start with an object
How would you solve this problem? Thank you!
You are missing the closing brace wrapping the item:
[
{
"name": {
"first": "Dickerson",
"last": "Wood"
}, etc.
]
You can validate your JSON e.g. via http://jsonlint.com/.
Also, try taking a look at http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html

Categories

Resources