I am currently working with the global search part of my application in which i am using elastic search i like to implement case sensitive search along with partial search
This is my json file
{
"settings": {
"analysis": {
"normalizer": {
"cpy_normalizer": {
"type": "custom",
"char_filter": [],
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"entityLabel": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"normalizer": "cpy_normalizer",
"ignore_above": 256
},
"displaycolumnval": {
"type": "keyword",
"ignore_above": 256
},
"case_sensitive": {
"type": "keyword",
"ignore_above": 256
}
}
},
"entityDescription": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"normalizer": "cpy_normalizer",
"ignore_above": 256
},
"displaycolumnval": {
"type": "keyword",
"ignore_above": 256
},
"case_sensitive": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
And this my code
public Document getEntityData(String date, ElasticSearchModel elasticSearchModel, String sortBy, String sortingOrder,
int pageSize, boolean isGlobalSearch, String username,boolean isCaseSensitive) {
Document searchEntity = new Document();
List<Query> queryList = new ArrayList<>();
String indexName = EntityData.INDEX_NAME;
// Fetching Data From JSON File .
List<String> fields = elastic_utils.getDataFromJsonFile("elastic/GlobalSearchFields/EntityData.json", "fields");
List<String> integerFields = elastic_utils.getDataFromJsonFile("elastic/GlobalSearchFields/EntityData.json", "integerFields");
List<String> globalSearchFields = elastic_utils.getDataFromJsonFile("elastic/GlobalSearchFields/EntityData.json", "globalSearchFields");
List<String> caseSensitiveFields = elastic_utils.getDataFromJsonFile("elastic/GlobalSearchFields/EntityData.json", "caseSensitiveFields");
// pit Id - pagination
String pitId;
pitId = elasticSearchModel.getPitId();
if (elasticSearchModel.getPitId().isBlank()) {
pitId = elastic_utils.createPitId(indexName);
} else {
if (elastic_utils.isPitIdExpired(pitId)) {
pitId = elasticSearchModel.getPitId();
} else {
pitId = elastic_utils.createPitId(indexName);
}
}
String finalPitId = pitId;
// Report Date Added - Query :-
Query reportDate = TermQuery.of(q -> q.field("reportDate").value(date))._toQuery();
queryList.add(reportDate);
// searchBox - Global Search
if (isGlobalSearch && elasticSearchModel.getSearchTerm() != null && !elasticSearchModel.getSearchTerm().isEmpty()) {
if (!isCaseSensitive){
queryList.add(elastic_utils.getGlobalSearchTerm(elasticSearchModel.getSearchTerm(), fields, integerFields, globalSearchFields));
}
else {
queryList.add(elastic_utils.getGlobalSearchTerm(elasticSearchModel.getSearchTerm(), caseSensitiveFields, integerFields, globalSearchFields));
}
} else {
if (elasticSearchModel.getSearchTerm() != null && !elasticSearchModel.getSearchTerm().isEmpty()) {
queryList.add(elastic_utils.getTranslateQuery(elasticSearchModel.getSearchTerm(), indexName));
}
}
public Query getGlobalSearchTerm(String searchTerm, List<String> fields, List<String> newDateFields,List<String> globalSearchFields) {
Query finalQuery = null;
if (!searchTerm.contains("AND ") || (!searchTerm.contains("OR"))) {
if (isDateField(searchTerm)) {
Query newSearchTerm = QueryStringQuery.of(q -> q.query(searchTerm).fields(newDateFields))._toQuery();
finalQuery = BoolQuery.of(b -> b.should(newSearchTerm))._toQuery();
} else if (isNumericField(searchTerm)) {
Query newSearchTerm = QueryStringQuery.of(q -> q.query(searchTerm).fields(globalSearchFields))._toQuery();
finalQuery = BoolQuery.of(b -> b.should(newSearchTerm))._toQuery();
} else if (globalSearchFields(searchTerm)) {
Query newSearchTerm = QueryStringQuery.of(q -> q.query(searchTerm).fields(globalSearchFields))._toQuery();
finalQuery = BoolQuery.of(b -> b.should(newSearchTerm))._toQuery();
} else {
Query newSearchTerm = QueryStringQuery.of(q -> q.query(searchTerm).fields(fields))._toQuery();
finalQuery = BoolQuery.of(b -> b.should(newSearchTerm))._toQuery();
}
} else {
Query newSearchTerm = QueryStringQuery.of(q -> q.query(searchTerm).fields(fields))._toQuery();
finalQuery = BoolQuery.of(b -> b.should(newSearchTerm))._toQuery();
}
log.info("Search term Query :- {}", finalQuery);
return finalQuery;
}
this is the json files i used for the fields
{
"fields": [
"entityLabel",
"entityDescription"
],
"caseSensitiveFields": [
"entityLabel.case_sensitive",
"entityDescription.case_sensitive"
],
"integerFields": [
],
"globalSearchFields": []
}
in this the case sensitive search is not working properly i don’t know why and what wrong with the code
i need the code to implement the case sensitive search
Harry Parker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.