Elastic search multiple regex search within same column in single request? - elasticsearch

Using java client for elastic search, I can able to use only one regex pattern search. For now, I'm using like this.
searchSourceBuilder.query(QueryBuilders.regexpQuery("name", "AB.*"));
But I need elastic search equivalent of
SELECT NAME FROM USERS WHERE NAME LIKE 'AB%' OR 'SID%'
I am using RestHighLevelClient. Is there any way to do multiple regex search within same column?
Thanks in advance.

You need to add both regexp queries inside a bool/should:
QueryBuilder first = QueryBuilders.regexpQuery("name", "AB.*");
QueryBuilder second = QueryBuilders.regexpQuery("name", "SID.*");
QueryBuilder boolQuery = QueryBuilders.boolQuery()
.should(first)
.should(second);
searchSourceBuilder.query(boolQuery);

Related

ElasticSearch: how to perform search across multiple fields using Java High Level REST Client?

I am looking at this tutorial and it describes how ES search could be executed against an index, but the search is done only using one field of each document:
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
I would like to perform my search against multiple fields: like user name, display name, email etc.
Should I use Multi-Search API to achieve it?
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-multi-search.html
MultiMatchQueryBuilder allows to do it, thanks to Abhay for helping me to find the proper solution.
https://snapshots.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.4.0-SNAPSHOT/org/elasticsearch/index/query/MultiMatchQueryBuilder.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html

How to combine multiple filters aggregation in Elasticsearch?

I am using Elasticsearch 5.3. In the Java client, I want to use filters on multiple fields using AND logic into a single bucket. Currently, the filter aggregation only supports single termQuery. How do I code multiple termQuery in the Java client?
AggregationBuilders.filter("specific_session", QueryBuilders.termQuery(FIELD_SESSION_ID, sessionId));
You need to use a bool query for that:
BoolQueryBuilder filters = QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery(FIELD_SESSION_ID, sessionId))
.filter(QueryBuilders.termQuery(FIELD_XYZ, xyz));
AggregationBuilders.filter("specific_session", filters);

Schemaless Elasticsearch not analyzed fields

I need to index documents dynamically, thus I am using schemaless mapping for elasticsearch.
But when i search for an exact match for a value like "ABC XYZ" against a string field, I do not get any hits.
Also I need my search to be case insensitive. Is it possible to acheive this, only by making changes while search like by specifying an analyzer.
I am new to elasticsearch so any help would be appreciated.
I found out the answer
QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("fieldName", "ABC XYZ");

Elasticsearch multi match exact match boosting

We use spring data elastic search.
We use multimatch query to search against multiple fields.
QueryBuilder queryBuilder = multiMatchQuery(searchInput, "id","firstName","lastName","title","nickName","location")
.type(MatchQueryBuilder.Type.PHRASE_PREFIX).analyzer("standard");
We use PHRASE_PREFIX for phrase match.
Now, we would like to add boosting for exact match on nickName( boosting only if exact match ).
For example, when i search for Phone, if there is a exact match on nickName, it should be ordered first in the result. If there is no exact match, then firstName should be given boosting. I tried boosting like below.
QueryBuilder queryBuilder = multiMatchQuery(searchInput, "id","firstName","lastName","title","nickName^9","location")
.type(MatchQueryBuilder.Type.PHRASE_PREFIX).analyzer("standard");
Boosting doesnt seem to be working. Also not sure, how to add boosting only on exact match on a field.
Thanks

Search for substring in a node name in JCR using CQ querybuilder

I am trying to do a search in CRXDE in CQ using the CQ qurybuilder tool to find all node names(in the tree structure) that contain the character '#' but am not successful. Being new to the Querybuilder tool, I am not aware of the exact conditions to be used to do the same.
Kindly help.
I tried the following query in the /bin/querybuilder.json tool-type=nt:file&path=/content/dam/marketinghub&property=nodename&property.value=#
I think the property=nodename part is wrong. What should I mention as property name when I have to search in the node names itself and not in any specific property of the node?
Almost there, but it is not the property that you need to search, it is the name of the node.
You have a nodename predicate that gets this job done for you.
It accepts a pattern that you would like to search for. So in your case, the query would be
type = nt:file
path = /content/dam/marketinghub
nodename = *#*
The json querybuilder url would be
/bin/querybuilder.json?nodename=*%23*&path=%2fcontent%2fdam%2fmarketinghub&type=nt%3afile
For further learning on query builder, refer this doc.

Resources