ElasticSearch Multi Term Query With Java High-Level REST Client - java

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

Related

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

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.

How to use MultiFieldQueryParser from Lucene?

I am using Version.Lucene_29. Using the normal string query method i could do the following:
Directory directory = new FSDirectory(...);
//Start Lucene retrieval.
IndexSearcher iSearch = new IndexSearcher(directory, true);
Analyzer analyzer = new WhitespaceAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_29, "content", analyzer);
String str = 'filename:testfile.txt AND filetext:"Singapore food"'
Query query = parser.parse(str);
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;
How do i fire a query using MultiFieldQueryParser in Lucene similar to the string query method?
MultiFieldQueryParser multiParser = new MultiFieldQueryParser(
Version.LUCENE_29, new String[] {"content", "ne"}, analyzer);
str = ???
Query = ????
ScoreDoc[] hits = iSearch.search(query, 1000).scoreDocs;
MultiFieldQueryParser allows you to search for a "WORD" in more then one Fileds with same Analyzer.
e.g.
Query query = MultiFieldQueryParser.parse("development",
new String[]{"title", "subject"},
new SimpleAnalyzer());
it will look for word development in Field : "title" and Field : "subject"
MultiFieldQueryParser is-a QueryParser, MultiFieldQueryParser creates the two Queries into a BooleanClause in this case. So it also supports filename:testfile.txt AND filetext:"Singapore food".

Categories

Resources