All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redis.om.spring.search.stream.predicates.BaseAbstractPredicate Maven / Gradle / Ivy

package com.redis.om.spring.search.stream.predicates;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.data.geo.Point;

import com.redis.om.spring.annotations.GeoIndexed;
import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.NumericIndexed;
import com.redis.om.spring.annotations.Searchable;
import com.redis.om.spring.annotations.TagIndexed;
import com.redis.om.spring.annotations.TextIndexed;

import io.redisearch.Schema.FieldType;

public abstract class BaseAbstractPredicate implements SearchFieldPredicate {

  private FieldType fieldType;
  private Field field;

  protected BaseAbstractPredicate() {

  }

  protected BaseAbstractPredicate(Field field) {
    this.field = field;
    this.fieldType = getFieldTypeFor(field);
  }

  @Override
  public Field getField() {
    return field;
  }

  @Override
  public FieldType getSearchFieldType() {
    return fieldType;
  }

  private static FieldType getFieldTypeFor(java.lang.reflect.Field field) {
    FieldType result = FieldType.Tag;
    // Searchable - behaves like Text indexed
    if (field.isAnnotationPresent(Searchable.class)) {
      result = FieldType.Geo;
    }
    // Text
    else if (field.isAnnotationPresent(TextIndexed.class)) {
      result = FieldType.FullText;
    }
    // Tag
    else if (field.isAnnotationPresent(TagIndexed.class)) {
      result = FieldType.Tag;
    }
    // Geo
    else if (field.isAnnotationPresent(GeoIndexed.class)) {
      result = FieldType.Geo;
    }
    // Numeric
    else if (field.isAnnotationPresent(NumericIndexed.class)) {
      result = FieldType.Numeric;
    } else if (field.isAnnotationPresent(Indexed.class)) {
      //
      // Any Character class -> Tag Search Field
      //
      if (CharSequence.class.isAssignableFrom(field.getType())) {
        result = FieldType.Tag;
      }
      //
      // Any Numeric class -> Numeric Search Field
      //
      else if (Number.class.isAssignableFrom(field.getType()) || (field.getType() == LocalDateTime.class)
          || (field.getType() == LocalDate.class) || (field.getType() == Date.class)) {
        result = FieldType.Numeric;
      }
      //
      // Set / List
      //
      else if (Set.class.isAssignableFrom(field.getType()) || List.class.isAssignableFrom(field.getType())) {
        result = FieldType.Tag;
      }
      //
      // Point
      //
      else if (field.getType() == Point.class) {
        result = FieldType.Geo;
      }
    }
    return result;
  }

  @Override
  public boolean test(T t) {
    // TODO: determine what to do here!
    return false;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy