I use Elasticsearch Java rest client 6.1. When I try to find logs due to a few parametrs than can be in one field I get nothing
Here is my code:
@Override
public SearchResponse findLogsByValues(ElasticSearchLogRequest esLogRequest, Pageable pageable) {
SearchRequest searchRequest = new SearchRequest("portal-logs-*");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
if (esLogRequest.getLevels() != null) {
Iterator<String> iterator = esLogRequest.getLevels().iterator();
int counter = 0;
SpanOrQueryBuilder spanOrQueryBuilder = null;
while (iterator.hasNext()) {
if (counter == 0) {
spanOrQueryBuilder = new SpanOrQueryBuilder(QueryBuilders.
spanTermQuery("level", iterator.next().toLowerCase()));
} else {
spanOrQueryBuilder.addClause(QueryBuilders.
spanTermQuery("level", iterator.next().toLowerCase()));
}
counter++;
}
bqb.filter(spanOrQueryBuilder);
}
try {
searchResponse = client.search(searchRequest);
} catch (IOException e) {
e.printStackTrace();
}
return searchResponse;
}
Here is my json request:
{
"levels": ["TRACE","INFO"]
}
Here is my mapping template:
PUT _template/portal-logs
{
"template": "portal-logs-*",
"settings": { "number_of_shards": 5 },
"mappings": {
"logs_info": {
"_all": {
"enabled": false
},
"properties": {
"device": {"type": "keyword"},
"header": {"type": "text"},
"ip": {"type": "keyword"},
"level": {"type": "keyword"},
"location": {"type": "geo_point"},
"message": {"type": "text"},
"module": {"type": "keyword"},
"node": {"type": "keyword"},
"office": {"type": "keyword"},
"operation": {"type": "keyword"},
"port": {"type": "integer"},
"sessionId": {"type": "keyword"},
"submodule": {"type": "keyword"},
"system": {"type": "keyword"},
"thread": {"type": "keyword"},
"timeStamp": {"type": "date"},
"userLogin": {"type": "keyword"},
"userName": {"type": "keyword"}
}
}
}
}
So when in mapping field "level" and set it as "text" - it works fine but when I set "keyword" - I receive an empty json.
I need that field "level" has a strict type "keyword" and it has to work when I want to get all logs that have "level" "INFO" or "TRACE".
What should I do in such case? Why with keyword it's not working?