Fetch the fields from an ElasticSearch 7.3. document with Springboot java - java

I am using the following java code to query the elasticsearch 7.3 and get 1 document. I have set it to only 1 doc with the highest score. It is working fine and returning me the doc perfectly.
#Autowired
RestHighLevelClient client;
#RequestMapping(value = "/search", method = RequestMethod.GET)
public #ResponseBody
String getItem(#RequestParam("string") String string) throws IOException {
QueryBuilder matchQueryBuilder = QueryBuilders.simpleQueryStringQuery(string);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.from(0);
sourceBuilder.size(1);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest("nutrients");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse.toString();
}
The response is as follow, I want to access the values using java code of Calories , Fat , Protein , Carbohydrate and ignore the values of other Nutrient.
I need the values in 4 variables like String caloriesVar=153 kcal and similarly for the other 3.
[

Related

Fetch all record including particular fields

I am working with Elasticcsearch 7.3. I want to fetch only two records of all the documents using JAVA Api from my index. I am using the following code but it returning the null object.
RestHighLevelClient client;
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.fetchSource("recipe_ID,recipe_url", null);
sourceBuilder.from(0);
SearchRequest searchRequest = new SearchRequest("recipes");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit searchHit = searchResponse.getHits().getAt(0);
String resultString = searchHit.getSourceAsString();
return resultString;
I need to include only two fields recipe_ID and recipe_url in my result.
You're on the right path, although source filtering requires you to specify your fields in an array like this:
String[] includeFields = new String[] {"recipe_ID", "recipe_url"};
sourceBuilder.fetchSource(includeFields, null);

ElasticSearch Java High Level REST Client: filter documents and or query

I need to retrieve documments filtered by "brand" attribute, and retrieve those that have an specic value for another attribute. Example:
All "apple" documents ("brand" attribute) that are active ("active" attribute true), with id "abc" , "def" or "ghi" values.
I am using Java High Level REST Client.
That is my code:
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/* add query filters */
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("brand", brand);
searchSourceBuilder.query(matchQueryBuilder);
/* base sort is added */
//TODO: load sort from mapping based on baseSort of customSort
searchSourceBuilder.sort(sortBy , SortOrder.DESC);
/* add product id */
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery("active", true));
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
Thank you!!
SearchRequest searchRequest = new SearchRequest(INDEX);
QueryBuilder qb = QueryBuilders.queryStringQuery(searchText + "*").defaultField("companyName")
.defaultOperator(Operator.AND);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("categories.id", category)
.should(QueryBuilders.termQuery("product_id", productIdsList)
);
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
searchRequest.source(sourceBuilder);

Elasticsearch - Java RestHighLevelClient - how to get all documents using scroll api

In my index in Elasticsearch I saved about 30000 entities. I'd like to get all ids of them using RestHighLevelClient. I've read that the best way to do it is to use scroll api. However when I do it I recieve only about 10 entities instead of 30k. How to solve this
final class ElasticRepo {
private final RestHighLevelClient restHighLevelClient;
List<ListingsData> getAllListingsDataIds() {
val request = new SearchRequest(ELASTICSEARCH_LISTINGS_INDEX);
request.types(ELASTICSEARCH_TYPE);
val searchSourceBuilder = new SearchSourceBuilder()
.query(matchAllQuery())
.fetchSource(new String[]{"listing_id"}, new String[]{"backoffice_data", "search_and_match_data"});
request.source(searchSourceBuilder);
request.scroll(TimeValue.timeValueMinutes(3));
return executeQuery(request);
}
private List<ListingsData> executeQuery(final SearchRequest searchQuery) {
try {
val hits = restHighLevelClient.search(searchQuery, RequestOptions.DEFAULT).getHits().getHits();
return Arrays.stream(hits).map(SearchHit::getSourceAsString).map(ElasticRepo::toListingsData).collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("");
}
}
}
And when I do it executeQuery returns only about 11 entites. How to solve that, how to obtain all documents in index ?
try to follow this example, I am using this code and it works:
String query = "your query here";
QueryBuilder matchQueryBuilder = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder(query));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(matchQueryBuilder);
searchSourceBuilder.size(5000); //max is 10000
searchRequest.indices("your index here");
searchRequest.source(searchSourceBuilder);
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(10L));
searchRequest.scroll(scroll);
SearchResponse searchResponse = client.search(searchRequest);
String scrollId = searchResponse.getScrollId();
SearchHit[] allHits = new SearchHit[0];
SearchHit[] searchHits = searchResponse.getHits().getHits();
while (searchHits != null && searchHits.length > 0)
{
allHits = Helper.concatenate(allHits, searchResponse.getHits().getHits()); //create a function which concatenate two arrays
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
searchResponse = client.searchScroll(scrollRequest);
scrollId = searchResponse.getScrollId();
searchHits = searchResponse.getHits().getHits();
}
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest);
As part of Search API, by default the max documents retrieved is 10 unless the size field is specified.
The Search Scroll API documentation as part of Java REST High Level document has a nice sample code -> https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-search-scroll.html

How to find documents with specific field contains string using RestHighLevelClient

So, I can make simple via url:
<elastic_host>/indexname/_search?pretty=true&q=text:*str*
I want to do the same with RestHighLevelClient, but it dosn't work:
SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.types(INDEX_TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.matchQuery(IMAGE_TEXT_FIELD_NAME, "*str*" );
searchSourceBuilder.query(queryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
The equivalent query is not a match query but a query_string query, so you can replace the following line
QueryBuilder queryBuilder = QueryBuilders.matchQuery(IMAGE_TEXT_FIELD_NAME, "*str*" );
by this one
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("*str*").field(IMAGE_TEXT_FIELD_NAME);

Search by the field in ElasticSearch in Java?

I have objects indexed in elasticsearch:
{"id":"one","name":"John"}
{"id":"two","name":"Steve"}
I put them into elastic with index 'people', type 'human' and document '/id(one,two)/'
The task is to search records by 'name' in java using elasticsearch 6.2.4 with rest high level client.
Here is my code example:
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("name", name));
SearchRequest searchRequest = new SearchRequest("people");
searchRequest.types("human");
searchRequest.indices("name").source(sourceBuilder);
SearchResponse searchResponse;
RestHighLevelClient client = getClient();
searchResponse = client.search(searchRequest);
this is not working.
Need help in performing the search.
Please try below code.
SearchRequest searchRequest = new SearchRequest("people");
searchRequest.types("human");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("name", name"));
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
Here name must be in lowercase.

Categories

Resources