11 Kasım 2020 Çarşamba

SpringData ElasticSearch ElasticsearchRepository Arayüzü

Giriş
Bu arayüz ile üretilen metodlar ElasticSearch açısında "query_string" sorguları oluşturuyor.

Örnek - keyword
Elimizde şöyle bir kod olsun. Burada en öenmli şeylerden birisi FieldType enum değerlerini bilmek. Burada "Keyword" kullanılıyor. Açıklaması şöyle
- The text datatype is used for full-text search and any field mapped as text gets converted into individual words before being indexed.
- The keyword datatype is used for exact matches. The field doesn’t get analyzed and gets stored as it is.
- Elasticsearch has removed the string datatype in versions 5.0 and above and introduced two new datatypes text and keyword. Any string field gets analyzed with both these datatypes. However you can modify the behavior.
Yani bu alan tokenize edilmeyecek 
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "foo_message")
public class Foo {

    @Id
    @Field(type = FieldType.Keyword)
    @JsonProperty("id")
    public String id;

    @JsonProperty("message")
    public String message;

    @JsonProperty("senderUsername")
    public String senderUsername;

    @JsonProperty("senderUserId")
    public Long senderUserId;

    @JsonProperty("receiverUserId")
    public Long receiverUserId;
}
Şöyle yaparız
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface KafkaMessageRepository extends ElasticsearchRepository<Foo, String> {
List<Foo> findAllBySenderUserIdAndReceiverUserIdOrderById(Long senderId, Long receiverId);
}
Örnek - text
Şöyle yaparız
@Document(indexName = "productindex")
public class Product {
  @Id
  private String id;
  
  @Field(type = FieldType.Text, name = "name")
  private String name;
  
  @Field(type = FieldType.Double, name = "price")
  private Double price;
  
  @Field(type = FieldType.Integer, name = "quantity")
  private Integer quantity;
  
  @Field(type = FieldType.Keyword, name = "category")
  private String category;
  
  @Field(type = FieldType.Text, name = "desc")
  private String description;
  
  @Field(type = FieldType.Keyword, name = "manufacturer")
  private String manufacturer;
  ...
}

Hiç yorum yok:

Yorum Gönder