Привет! У меня эластичный поисковый индекс с именем mep-reports-today. ниже показано, как это выглядит.
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 100,
"max_score": 1.0,
"hits": [
{
"_index": "mep-reports-today",
"_type": "doc",
"_id": "Rta62G0Bc1ykGt-JpwfK",
"_score": 1.0,
"_source": {
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-09T15:47:48.778Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": "420736408283",
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": "8800111",
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"submission_ts": 1568105380000,
"delivery_ts": 1567491477000,
"campaign_name": "Munchies Ireland Sandwitch and drinks promotion",
"flight_name": "Very Very long long flight"
}
},
{
"_index": "mep-reports-today",
"_type": "doc",
"_id": "SNa62G0Bc1ykGt-JpwfL",
"_score": 1.0,
"_source": {
"inventory": "MMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-01T02:40:42.040Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": "420736408281",
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": "8800111",
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"submission_ts": 1568105380000,
"delivery_ts": 1565902976000,
"campaign_name": "Starbucks Promotion",
"flight_name": "Short Flight"
}
},
{
"_index": "mep-reports-today",
"_type": "doc",
"_id": "Sda62G0Bc1ykGt-JpwfL",
"_score": 1.0,
"_source": {
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-01T16:00:05.666Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": "420736408281",
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": "8800111",
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"submission_ts": 1568105380000,
"delivery_ts": 1558893228000,
"campaign_name": "Starbucks Promotion",
"flight_name": "Very Very long long flight"
}
},
{
"_index": "mep-reports-today",
"_type": "doc",
"_id": "Sta62G0Bc1ykGt-JpwfL",
"_score": 1.0,
"_source": {
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-03T17:09:50.380Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": "420736408281",
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": "8800111",
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"submission_ts": 1568105380000,
"delivery_ts": 1547471597000,
"campaign_name": "Munchies Ireland Sandwitch and drinks promotion",
"flight_name": "Very Very long long flight"
}
},
{
"_index": "mep-reports-today",
"_type": "doc",
"_id": "Tta62G0Bc1ykGt-JpwfL",
"_score": 1.0,
"_source": {
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-09T21:46:14.947Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": "420736408283",
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": "8800111",
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"submission_ts": 1568105380000,
"delivery_ts": 1559378049000,
"campaign_name": "Munchies Ireland Sandwitch and drinks promotion",
"flight_name": "Short Flight"
}
}
]
}
}
У меня есть экран поиска, где пользователь может искать в различных полях, таких как @timestamp, status, campaign_id, получатель, ресурс, flight_id, account_id. Я использую клиент упругого поиска Java для выдачи поискового запроса к упругому поиску. Однако это не работает, как ожидалось, когда мне нужно искать в поле состояния и инвентаря. Статус и инвентарь представляются пользователю как опция множественного выбора.
, вот как я строю запрос.
@ Открытый класс хранилища MessageHistoryReportingRepository {
public MessageHistorySearchResponse findAll(MessageHistoryFilterRequest messageHistoryFilterRequest) {
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(ELASTIC_SEARCH_INDEX);
SearchRequest searchRequest = searchRequestBuilder.from(messageHistoryFilterRequest.getPageNumber())
.size(messageHistoryFilterRequest.getPageSize()).timeout(300, TimeUnit.SECONDS)
.range(messageHistoryFilterRequest.getStartDate(), messageHistoryFilterRequest.getEndDate())
.msisdn(messageHistoryFilterRequest.getMsisdn()).accountId(messageHistoryFilterRequest.getAccountId())
.campaignId(messageHistoryFilterRequest.getCampaignId())
.flightId(messageHistoryFilterRequest.getFlightId())
.cdrStatus(messageHistoryFilterRequest.getCdrStatus())
.inventoryCode(messageHistoryFilterRequest.getInventoryCode()).sort().build();
SearchResponse searchResponse = null;
List<MessageHistory> messageHistories = null;
try {
searchResponse = elasticSearchClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
List<SearchHit> searchHitResults = Arrays.asList(searchHits.getHits());
messageHistories = searchHitResults.stream().map(this::translate).collect(Collectors.toList());
return new MessageHistorySearchResponse(messageHistories, messageHistoryFilterRequest.getPageSize(),
messageHistoryFilterRequest.getPageNumber(), searchHits.getTotalHits());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static class SearchRequestBuilder {
private SearchRequest searchRequest;
private SearchSourceBuilder searchSourceBuilder;
private BoolQueryBuilder boolQueryBuilder;
SearchRequestBuilder(String elasticSearchIndex) {
this.searchRequest = new SearchRequest(elasticSearchIndex);
this.searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
this.searchSourceBuilder = new SearchSourceBuilder();
this.searchRequest.source(searchSourceBuilder);
this.boolQueryBuilder = QueryBuilders.boolQuery();
this.searchSourceBuilder.query(this.boolQueryBuilder);
}
SearchRequestBuilder from(Integer pageNumber) {
this.searchSourceBuilder.from(pageNumber);
return this;
}
SearchRequestBuilder size(Integer pageSize) {
this.searchSourceBuilder.size(pageSize);
return this;
}
SearchRequestBuilder timeout(long duration, TimeUnit timeUnit) {
this.searchSourceBuilder.timeout(new TimeValue(duration, TimeUnit.SECONDS));
return this;
}
SearchRequestBuilder range(ZonedDateTime startDate, ZonedDateTime endDate) {
RangeQueryBuilder startRangeQueryBuilder = QueryBuilders.rangeQuery("@timestamp").gte(startDate);
startRangeQueryBuilder.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
boolQueryBuilder.must(startRangeQueryBuilder);
RangeQueryBuilder endRangeQueryBuilder = QueryBuilders.rangeQuery("@timestamp").lte(endDate.plusDays(1l));
boolQueryBuilder.must(endRangeQueryBuilder);
endRangeQueryBuilder.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
return this;
}
SearchRequestBuilder msisdn(String msisdn) {
if (msisdn != null) {
if (msisdn.indexOf("*") >= 0) {
WildcardQueryBuilder msisdnWildCardQueryBuilder = new WildcardQueryBuilder("recipient", msisdn);
boolQueryBuilder.must(msisdnWildCardQueryBuilder);
} else {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("recipient", msisdn);
boolQueryBuilder.must(matchQueryBuilder);
}
}
return this;
}
SearchRequestBuilder accountId(UUID accountId) {
if (accountId != null) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("account_id", accountId.toString());
boolQueryBuilder.must(matchQueryBuilder);
}
return this;
}
SearchRequestBuilder campaignId(UUID campaignId) {
if (campaignId != null) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("campaign_id", campaignId.toString());
boolQueryBuilder.must(matchQueryBuilder);
}
return this;
}
SearchRequestBuilder flightId(UUID flightId) {
if (flightId != null) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("flight_id", flightId.toString());
boolQueryBuilder.must(matchQueryBuilder);
}
return this;
}
SearchRequestBuilder cdrStatus(List<String> cdrStatus) {
if (cdrStatus != null && cdrStatus.size() > 0) {
for(String cdrStatusCode:cdrStatus) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("status", cdrStatusCode);
boolQueryBuilder.should(matchQueryBuilder);
}
}
return this;
}
SearchRequestBuilder inventoryCode(List<String> inventoryCode) {
if (inventoryCode != null && inventoryCode.size() > 0) {
for(String inventoryCodeValue:inventoryCode) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("inventory", inventoryCodeValue);
boolQueryBuilder.should(matchQueryBuilder);
}
}
// if (inventoryCode != null) {
// MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("inventory", inventoryCode);
// boolQueryBuilder.must(matchQueryBuilder);
// }
return this;
}
SearchRequestBuilder sort() {
searchSourceBuilder.sort(new FieldSortBuilder("@timestamp").order(SortOrder.DESC));
return this;
}
SearchRequestBuilder scroll(TimeValue value) {
searchRequest.scroll(new Scroll(value));
return this;
}
SearchRequestBuilder fetchSourceContext(boolean fetchSource, String[] includes, String[] excludes) {
FetchSourceContext fetchContext = new FetchSourceContext(fetchSource, includes, excludes);
this.searchSourceBuilder.fetchSource(fetchContext);
return this;
}
SearchRequest build() {
return this.searchRequest;
}
}
}
то, что я пытаюсь достичь, - это выполнить условия на основе пользовательских данных, как указано ниже.
@timestamp > (input start date ) and @timestamp < (input end date) and campaign_id = 6f2abca3-b46d-43f3-91be-3278a8dd7dc0 and recipient = "420736408283" and inventory in ( 'SMS', 'MMS') and status in ( 'ENROUTE' , 'DELIVERED').
однако он не работает из-за множественного выбора полей инвентаря и статуса. Любая идея, как я могу переписать мой класс SearchRequestBuilder.
SearchRequestBuilder cdrStatus(List<String> cdrStatus) {
if (cdrStatus != null && cdrStatus.size() > 0) {
for(String cdrStatusCode:cdrStatus) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("status", cdrStatusCode);
boolQueryBuilder.should(matchQueryBuilder);
}
}
return this;
}
SearchRequestBuilder inventoryCode(List<String> inventoryCode) {
if (inventoryCode != null && inventoryCode.size() > 0) {
for(String inventoryCodeValue:inventoryCode) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("inventory", inventoryCodeValue);
boolQueryBuilder.should(matchQueryBuilder);
}
}
// if (inventoryCode != null) {
// MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("inventory", inventoryCode);
// boolQueryBuilder.must(matchQueryBuilder);
// }
return this;
}
большое спасибо