1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
| package app.pooi.redissearch.search;
import app.pooi.redissearch.search.anno.CreateIndex; import app.pooi.redissearch.search.anno.Field; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import lombok.Data; import org.apache.commons.lang3.ArrayUtils; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisZSetCommands; import org.springframework.data.redis.core.*; import org.springframework.data.redis.hash.Jackson2HashMapper; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.*; import reactor.util.function.Tuple2; import reactor.util.function.Tuples;
import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream;
import static app.pooi.redissearch.search.SearchCore.Util.*;
@RestController @Service public class SearchCore {
private StringRedisTemplate redisTemplate;
private Jackson2HashMapper hashMapper = new Jackson2HashMapper(true);
@Data private static class Person { private Long id; private String name; private Integer age; private Long ctime; }
@PostMapping("/person") @CreateIndex( index = "person", documentId = "#p0.id", fields = { @Field(propertyName = "name", value = "#p0.name"), @Field(propertyName = "age", value = "#p0.age", sort = true), @Field(propertyName = "ctime", value = "#p0.ctime", sort = true) }) Person addPerson(Person person) { return person; }
public SearchCore(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; }
public void indexMeta(String index, Map<String, FieldMeta> fieldMeta) { this.redisTemplate.opsForHash().putAll(genIdxMetaName(index), hashMapper.toHash(fieldMeta)); }
@PostMapping("/index") public int indexDocument( final String index, final String field, final String documentId, final String document) { return this.indexDocument(index, field, documentId, document, doc -> Lists.newArrayList(doc.split(""))); }
public int indexDocument( final String index, final String field, final String documentId, final String document, final Function<String, List<String>> tokenizer) {
final List<String> tokens = tokenizer != null ? tokenizer.apply(document) : Collections.singletonList(document);
final String docKey = genDocIdxName(index, documentId);
final List<Object> results = redisTemplate.executePipelined(new SessionCallback<Integer>() { @Override public Integer execute(RedisOperations operations) throws DataAccessException { final StringRedisTemplate template = (StringRedisTemplate) operations;
final String[] idxs = tokens.stream() .map(word -> genIdxName(index, field, word)) .peek(idx -> ((StringRedisTemplate) operations).opsForSet().add(idx, documentId)) .toArray(String[]::new);
template.opsForSet().add(docKey, idxs); return null; } }); return results.size(); }
public int indexSortField( final String index, final String field, final String documentId, final Double document) {
final String docKey = genDocIdxName(index, documentId);
final List<Object> results = redisTemplate.executePipelined(new SessionCallback<Integer>() { @Override public Integer execute(RedisOperations operations) throws DataAccessException { final StringRedisTemplate template = (StringRedisTemplate) operations; final String idxName = genSortIdxName(index, field); template.opsForZSet().add(idxName, documentId, document); template.opsForSet().add(docKey, idxName); return null; } }); return results.size(); }
@DeleteMapping("/index") public int deleteDocumentIndex(final String index, final String documentId) { final String docKey = genDocIdxName(index, documentId); final Boolean hasKey = redisTemplate.hasKey(docKey); if (!hasKey) { return 0; }
final List<Object> results = redisTemplate.executePipelined(new SessionCallback<Integer>() { @Override public Integer execute(RedisOperations operations) throws DataAccessException { final Set<String> idx = redisTemplate.opsForSet().members(docKey); ((StringRedisTemplate) operations).delete(idx); ((StringRedisTemplate) operations).delete(docKey); return null; } }); return results.size(); }
@PatchMapping("/index") public int updateDocumentIndex(final String index, final String field, final String documentId, final String document) { this.deleteDocumentIndex(index, documentId); return this.indexDocument(index, field, documentId, document); }
public int updateSortField(final String index, final String field, final String documentId, final Double document) { this.deleteDocumentIndex(index, documentId); return this.indexSortField(index, field, documentId, document); }
private Consumer<SetOperations<String, String>> operateAndStore(String method, String key, Collection<String> keys, String destKey) { switch (method) { case "intersectAndStore": return (so) -> so.intersectAndStore(key, keys, destKey); case "unionAndStore": return (so) -> so.unionAndStore(key, keys, destKey); case "differenceAndStore": return (so) -> so.differenceAndStore(key, keys, destKey); default: return so -> { }; } }
private Consumer<ZSetOperations<String, String>> zOperateAndStore(String method, String key, Collection<String> keys, String destKey, final RedisZSetCommands.Weights weights) { switch (method) { case "intersectAndStore": return (so) -> so.intersectAndStore(key, keys, destKey, RedisZSetCommands.Aggregate.SUM, weights); case "unionAndStore": return (so) -> so.unionAndStore(key, keys, destKey, RedisZSetCommands.Aggregate.SUM, weights); default: return so -> { }; } }
private String common(String index, String method, List<String> keys, long ttl) { final String destKey = Util.genQueryIdxName(index);
redisTemplate.executePipelined(new SessionCallback<String>() { @Override public <K, V> String execute(RedisOperations<K, V> operations) throws DataAccessException { operateAndStore(method, keys.stream().limit(1L).findFirst().get(), keys.stream().skip(1L).collect(Collectors.toList()), destKey) .accept(((StringRedisTemplate) operations).opsForSet()); ((StringRedisTemplate) operations).expire(destKey, ttl, TimeUnit.SECONDS); return null; } }); return destKey; }
public String intersect(String index, List<String> keys, long ttl) { return common(index, "intersectAndStore", keys, ttl); }
public String union(String index, List<String> keys, long ttl) { return common(index, "unionAndStore", keys, ttl); }
public String diff(String index, List<String> keys, long ttl) { return common(index, "differenceAndStore", keys, ttl); }
private static Tuple2<Set<Tuple2<String, String>>, Set<Tuple2<String, String>>> parse(String query) {
final Pattern pattern = Pattern.compile("[+-]?([\\w\\d]+):(\\S+)");
final Matcher matcher = pattern.matcher(query);
Set<Tuple2<String, String>> unwant = Sets.newHashSet(); Set<Tuple2<String, String>> want = Sets.newHashSet();
while (matcher.find()) { String word = matcher.group();
String prefix = null; if (word.length() > 1) { prefix = word.substring(0, 1); }
final Tuple2<String, String> t = Tuples.of(matcher.group(1), matcher.group(2)); if ("-".equals(prefix)) { unwant.add(t); } else { want.add(t); } } return Tuples.of(want, unwant); }
public String query( String index, String query) {
final Tuple2<Set<Tuple2<String, String>>, Set<Tuple2<String, String>>> parseResult = parse(query); final Set<Tuple2<String, String>> want = parseResult.getT1(); final Set<Tuple2<String, String>> unwant = parseResult.getT2();
if (want.isEmpty()) { return ""; }
final Map<String, FieldMeta> entries = (Map<String, FieldMeta>) hashMapper.fromHash(redisTemplate.<String, Object>opsForHash().entries(genIdxMetaName(index)));
final List<Tuple2<String, String>> unionFields = want.stream() .filter(w -> w.getT2().contains(",")) .filter(w -> "true".equals(entries.get(w.getT1()).getSort())) .collect(Collectors.toList()); final List<String> unionIdx = unionFields.stream() .flatMap(w -> Arrays.stream(w.getT2().split(",")).map(value -> Tuples.of(w.getT1(), value))) .map(w -> genIdxName(index, w.getT1(), w.getT2())) .collect(Collectors.toList());
final String unionResultId = unionIdx.isEmpty() ? "" : this.union(index, unionIdx, 30L);
want.removeAll(unionFields);
final List<String> intersectIdx = want.stream() .flatMap(t -> { if ("true".equals(entries.get(t.getT1()).getSort())) return Stream.of(t); return Arrays.stream(t.getT2().split("")).map(value -> Tuples.of(t.getT1(), value)); }) .map(w -> genIdxName(index, w.getT1(), w.getT2())) .collect(Collectors.toList());
if (!unionResultId.isEmpty()) intersectIdx.add(unionResultId);
String intersectResult = this.intersect(index, intersectIdx, 30L);
return unwant.isEmpty() ? intersectResult : this.diff(index, Stream.concat(Stream.of(intersectResult), unwant.stream().map(w -> genIdxName(index, w.getT1(), w.getT2()))).collect(Collectors.toList()), 30L); }
@GetMapping("/query/{index}") public Set<String> queryAndSort( @PathVariable("index") String index, @RequestParam("param") String query, @RequestParam("sort") String sort, Integer start, Integer stop ) { final String[] sorts = sort.split(" ");
final Map<String, Integer> map = Arrays.stream(sorts).collect( Collectors.toMap(f -> { if (f.startsWith("+") || f.startsWith("-")) { f = f.substring(1); } return genSortIdxName("person", f); }, field -> field.startsWith("-") ? -1 : 1) );
final int[] weights = map.values() .stream() .mapToInt(Integer::intValue) .toArray();
String queryId = this.query(index, query); Long size; if (queryId.length() == 0 || (size = redisTemplate.opsForSet().size(queryId)) == null || size == 0) { return Collections.emptySet(); }
final String resultId = genQueryIdxName(index);
redisTemplate.executePipelined(new SessionCallback<Object>() { @Override public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException { final StringRedisTemplate template = (StringRedisTemplate) operations;
SearchCore.this.zOperateAndStore("intersectAndStore", map.keySet().stream().limit(1L).findFirst().get(), Stream.concat(map.keySet().stream().skip(1L), Stream.of(queryId)).collect(Collectors.toList()), resultId, RedisZSetCommands.Weights.of(ArrayUtils.add(weights, 0))).accept(template.opsForZSet());
template.expire(resultId, 30L, TimeUnit.SECONDS);
return null; } });
return redisTemplate.opsForZSet().range(resultId, start, stop);
}
static class Util {
private Util() { }
static String genIdxMetaName(String index) { return String.format("meta:idx:%s", index); }
static String genIdxName(String index, String field, String value) { return String.format("idx:%s:%s:%s", index, field, value); }
static String genSortIdxName(String index, String field) { return String.format("idx:%s:%s", index, field); }
static String genQueryIdxName(String index) { return String.format("idx:%s:q:%s", index, UUID.randomUUID().toString()); }
static String genDocIdxName(String index, String documentId) { return String.format("doc:%s:%s", index, documentId); } } }
|