Skip to content

Commit 15ff63a

Browse files
committed
Add ElasticSearchLiteQueryCreator
1 parent 10737ed commit 15ff63a

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ New properties are:
1818
(along with a "default"
1919
[GenericGetQueryCreator](src/main/java/com/getindata/connectors/http/internal/table/lookup/querycreators/GenericGetQueryCreator.java)
2020
implementation) for customization of queries prepared by Lookup Source for its HTTP requests.
21+
- Add [ElasticSearchLiteQueryCreator](src/main/java/com/getindata/connectors/http/internal/table/lookup/querycreators/ElasticSearchLiteQueryCreator.java)
22+
that prepares [`q` parameter query](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-api-query-params-q)
23+
using Lucene query string syntax (in first versions of ElasticSearch called
24+
[Search _Lite_](https://www.elastic.co/guide/en/elasticsearch/guide/current/search-lite.html)).
2125

2226
## [0.4.0] - 2022-08-31
2327

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.getindata.connectors.http.internal.table.lookup.querycreators;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
import com.getindata.connectors.http.LookupArg;
7+
import com.getindata.connectors.http.LookupQueryCreator;
8+
9+
/**
10+
* A {@link LookupQueryCreator} that prepares <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-api-query-params-q"><code>q</code> parameter query</a>
11+
* for ElasticSearch <i>Search API</i> using Lucene query string syntax (in first versions of the
12+
* ElasticSearch called <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/search-lite.html">Search <i>Lite</i></a>).
13+
*/
14+
public class ElasticSearchLiteQueryCreator implements LookupQueryCreator {
15+
private static final String ENCODED_SPACE = "%20";
16+
private static final String ENCODED_QUOTATION_MARK = "%22";
17+
18+
@Override
19+
public String createLookupQuery(List<LookupArg> params) {
20+
var luceneQuery = params.stream()
21+
.map(ElasticSearchLiteQueryCreator::processLookupArg)
22+
.collect(Collectors.joining(ENCODED_SPACE + "AND" + ENCODED_SPACE));
23+
24+
return luceneQuery.isEmpty() ? "" : ("q=" + luceneQuery);
25+
}
26+
27+
private static String processLookupArg(LookupArg arg) {
28+
return arg.getArgName()
29+
+ ":"
30+
+ ENCODED_QUOTATION_MARK
31+
+ arg.getArgValue()
32+
+ ENCODED_QUOTATION_MARK;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.getindata.connectors.http.internal.table.lookup.querycreators;
2+
3+
import java.util.Set;
4+
5+
import org.apache.flink.configuration.ConfigOption;
6+
7+
import com.getindata.connectors.http.LookupQueryCreator;
8+
import com.getindata.connectors.http.LookupQueryCreatorFactory;
9+
10+
/**
11+
* Factory for creating {@link ElasticSearchLiteQueryCreator}.
12+
*/
13+
public class ElasticSearchLiteQueryCreatorFactory implements LookupQueryCreatorFactory {
14+
public static final String IDENTIFIER = "elasticsearch-lite";
15+
16+
@Override
17+
public LookupQueryCreator createLookupQueryCreator() {
18+
return new ElasticSearchLiteQueryCreator();
19+
}
20+
21+
@Override
22+
public String factoryIdentifier() {
23+
return IDENTIFIER;
24+
}
25+
26+
@Override
27+
public Set<ConfigOption<?>> requiredOptions() {
28+
return Set.of();
29+
}
30+
31+
@Override
32+
public Set<ConfigOption<?>> optionalOptions() {
33+
return Set.of();
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
com.getindata.connectors.http.internal.table.lookup.HttpLookupTableSourceFactory
2+
com.getindata.connectors.http.internal.table.lookup.querycreators.ElasticSearchLiteQueryCreatorFactory
23
com.getindata.connectors.http.internal.table.lookup.querycreators.GenericGetQueryCreatorFactory
34
com.getindata.connectors.http.internal.table.sink.HttpDynamicTableSinkFactory
45
com.getindata.connectors.http.internal.table.sink.Slf4jHttpPostRequestCallbackFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.getindata.connectors.http.internal.table.lookup.querycreators;
2+
3+
import java.util.List;
4+
import java.util.stream.Stream;
5+
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
import com.getindata.connectors.http.LookupArg;
12+
13+
public class ElasticSearchLiteQueryCreatorTest {
14+
static Stream<Arguments> queryArguments() {
15+
return Stream.of(
16+
Arguments.of(List.of(), ""),
17+
Arguments.of(List.of(new LookupArg("key1", "val1")), "q=key1:%22val1%22"),
18+
Arguments.of(List.of(
19+
new LookupArg("key1", "val1"),
20+
new LookupArg("key2", "val2"),
21+
new LookupArg("key3", "3")
22+
), "q=key1:%22val1%22%20AND%20key2:%22val2%22%20AND%20key3:%223%22")
23+
);
24+
}
25+
26+
@ParameterizedTest
27+
@MethodSource("queryArguments")
28+
public void testGenericGetQueryCreation(List<LookupArg> args, String expectedQuery) {
29+
var queryCreator = new ElasticSearchLiteQueryCreator();
30+
var createdQuery = queryCreator.createLookupQuery(args);
31+
assertThat(createdQuery).isEqualTo(expectedQuery);
32+
}
33+
}

0 commit comments

Comments
 (0)