Skip to content

Commit

Permalink
Support for setting command timeout, logging fixes, update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimpza committed May 14, 2023
1 parent fa3a4ba commit 5db272e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Minimum Effort Search

A simple REST service exposing some RedisSearch functionality to support
simple self-hosted site search functionality.
simple self-hosted full-text search functionality.

## Runtime Requirements

- Java 17 JRE
- A [RediSearch](https://oss.redislabs.com/redisearch/) instance to connect to
- A [RediSearch](https://redis.io/docs/stack/search/) instance to connect to

## Build

Expand Down Expand Up @@ -57,6 +57,7 @@ file.
"title": "Blue T-Shirt",
"body": "A very basic blue t-shirt you can wear",
"price": 100,
"url": "my.site/shirts/1",
"tags": "shirt,blue,clothing"
}
}
Expand Down Expand Up @@ -107,6 +108,7 @@ Parameters:
"title": "Blue T-Shirt",
"body": "A very basic blue t-shirt you can wear",
"price": 100,
"url": "my.site/shirts/1",
"tags": "shirt,blue,clothing"
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'net.shrimpworks'
version = "1.5"
version = "1.6"

mainClassName = 'net.shrimpworks.mes.Main'

Expand Down
19 changes: 12 additions & 7 deletions src/main/java/net/shrimpworks/mes/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.search.IndexDefinition;
Expand All @@ -35,25 +37,27 @@ public static void main(String[] args) throws IOException {

Path configPath = Paths.get(args[0]).toAbsolutePath();
if (!Files.exists(configPath) || !Files.isRegularFile(configPath)) {
System.err.printf("Config file %s does not exist%n", configPath);
logger.error("Config file {} does not exist", configPath);
System.exit(3);
} else {
System.out.printf("Using configuration in file %s%n", configPath);
logger.info("Using configuration in file {}", configPath);
}

Config config = JacksonMapper.YAML.object(configPath, Config.class);
String[] redis = config.redisHost.split(":");
JedisPooled client = new JedisPooled(redis[0], redis.length > 1 ? Integer.parseInt(redis[1]) : 6379);
JedisPooled client = new JedisPooled(
HostAndPort.from(config.redisHost),
DefaultJedisClientConfig.builder().timeoutMillis(config.redisTimeoutMillis).build()
);
try {
client.ftCreate(
config.index,
IndexOptions.defaultOptions().setDefinition(new IndexDefinition().setPrefixes(config.prefix)),
config.schema.toSchema()
);
System.out.printf("Created index %s%n", config.index);
logger.info("Created index {}", config.index);
} catch (JedisDataException je) {
if (je.getMessage().contains("already exists")) {
System.out.printf("Index %s already exists, updating%n", config.index);
logger.info("Index {} already exists, updating", config.index);
// if the index already exists, we can make an attempt at adding fields (there's no api for deleting fields)
List<List<Object>> fields = (List<List<Object>>)client.ftInfo(config.index).get("attributes");
Set<String> fieldNames = fields.stream()
Expand Down Expand Up @@ -83,7 +87,7 @@ public static void main(String[] args) throws IOException {
}

public static void sampleConfig(PrintStream out) throws IOException {
Config config = new Config("example", "ex:", "localhost:6379", "0.0.0.0:8080", "", "*", UUID.randomUUID().toString(),
Config config = new Config("example", "ex:", "localhost:6379", 5000, "0.0.0.0:8080", "", "*", UUID.randomUUID().toString(),
new RediSearchSchema(Set.of(
new RediSearchField(Schema.FieldType.TEXT, "title", true, false, 5.0, false, null),
new RediSearchField(Schema.FieldType.TEXT, "body", false, false, 1.0, false, null),
Expand All @@ -97,6 +101,7 @@ public record Config(
String index,
String prefix,
String redisHost,
int redisTimeoutMillis,
String bindAddress,
String rootPath,
String corsAllowOrigins,
Expand Down

0 comments on commit 5db272e

Please sign in to comment.