How to find documents with specific field contains string using RestHighLevelClient - java

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);

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);

Is there an easy way to insert a term into Elasticsearch QueryDSL using Java?

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);

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 Multi Term Query With Java High-Level REST Client

The java-high-level-rest-client provides a method to search on elasticsearch using a term that Shown below its code
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
But I want to do a query on multiple fields like this: "user"="kimchy" and/or "city"="london".
I see the multi-search query and multi-match query but they don't do what I want.
Thanks for the help!!
try this:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder
.filter(QueryBuilders.termQuery("user", "kimchy"))
.filter(QueryBuilders.termQuery("city", "london"));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQueryBuilder);
the filter/must/should method on BoolQueryBuilder, depends on which context you want, if you want "or", you can use should.
One can try this.
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
foreach (var term in (s + ',' + t).Split(','))
{
booleanQuery.add(new TermQuery(new Term("content", term)), BooleanClause.Occur.SHOULD);
}
//booleanQuery.add(new TermQuery(new Term("content", s)), BooleanClause.Occur.MUST);
//booleanQuery.add(new TermQuery(new Term("content", t)), BooleanClause.Occur.MUST);
TopDocs hits = searcher.search(booleanQuery.build(), int.MaxValue);

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