Skip to content

okihouse/spring-boot-redis-auto-complete

Repository files navigation

Build Status Maven Central

Description

This project supports the automatic completion of the word.

Also, you can include points in the word. (like as related contents points for the Instagram)

Benchmark

Each step was tested 10 times. (10k words, 100k words, 100k fixed length words)

You can checkout the Autocomplete benchmark project for details and review/run the benchmarks yourself.

The average response rate is as follows :

1 Environments : Local(macbook), 2.5 GHz Intel Core i7, 16GB 1600 MHz DDR3
2 10k words: https://github.com/first20hours/google-10000-english
3 100k words: https://gist.github.com/h3xx/1976236

How to use?

Add the following dependency in pom.xml (Java 8 maven artifact)

<dependency>
  <groupId>com.github.okihouse</groupId>
  <artifactId>autocomplete</artifactId>
  <version>1.0.1</version>
</dependency>

Bean Configuration

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Bean(name = {"autocompleteKeyRepository", "keyRepository"})
public AutocompleteKeyRepository keyRepository() {
	AutocompleteKeyRepository keyRepository = new AutocompleteKeyServiceImpl(stringRedisTemplate);
	return keyRepository;
}

@Bean(name = {"autocompleteRepository"})
public AutocompleteRepository autocompleteRepository(AutocompleteKeyRepository autocompleteKeyRepository) {
	AutocompleteRepository autocompleteRepository = new AutocompleteServiceImpl(stringRedisTemplate, autocompleteKeyRepository);
	return autocompleteRepository;
}

Examples

@Autowired
private AutocompleteRepository autocompleteRepository;

@Test
public void autocomplete() throws Exception {
	String apple = "apple";

	// step1. clear a "apple"
	autocompleteRepository.clear(apple);

	// step2. Add a "apple"
	autocompleteRepository.add(apple);

	// step3. Get auto-complete words with prefix "a"
	List<AutocompleteData> autocompletes = autocompleteRepository.complete("a");

	Assert.assertNotNull(autocompletes);
	Assert.assertTrue(autocompletes.size() == 1);

	AutocompleteData autocompleteData = autocompletes.get(0);

	Assert.assertTrue(autocompleteData.getValue().equals(apple));
	Assert.assertTrue(autocompleteData.getScore() == 1);
}

Requirements

This project depend on spring-boot and spring-boot-redis (Prerequisites)

Note

  • There are many places to repair.
  • All contributions are welcome.
  • Please use the GitHub issue tracker if you have any ideas or bugs to report.

About

Autocomplete words with spring boot and redis

Resources

License

Stars

Watchers

Forks

Packages

No packages published