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.
Related
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);
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.
[
I am trying to take JSON from an external source and add one condition to it before running it as a query. I know I can do this:
var qb = QueryBuilders.wrapperQuery(json);
var searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(qb);
... but then I want to add must match (term:value) to this DSL
Is there a good way to do this in Java?
To be straight and simple:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
QueryBuilder qb = QueryBuilders.wrapperQuery("");
boolQueryBuilder.must(qb).must(QueryBuilders.termQuery("",""));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
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);
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);