10

मैं वसंत-डेटा-लोचदार खोज का उपयोग कर रहा हूं और शुरुआत के लिए सब कुछ ठीक काम करता है।स्प्रिंग डेटा लोचदार खोज: एक ही दस्तावेज़ के साथ एकाधिक सूचकांक

@Document(type = "products", indexName = "empty") 
public class Product 
{ 
... 
} 

public interface ProductRepository extends ElasticsearchRepository<Product, String> 
{ 
... 
} 

अपने मॉडल में मैं उत्पादों के लिए खोज सकते हैं।

@Autowired 
private ProductRepository repository; 
... 
repository.findByIdentifier("xxx").getCategory()); 

तो, मेरी समस्या है - मैं विभिन्न सूचकांकों में एक ही Elasticsearch प्रकार है और मैं सभी प्रश्नों के लिए एक ही दस्तावेज़ का उपयोग करना चाहते। मैं पूल के माध्यम से अधिक कनेक्शन संभाल सकता हूं - लेकिन मुझे नहीं पता कि मैं इसे कैसे कार्यान्वित कर सकता हूं। एक अलग सूचकांक के साथ

ProductRepository customerRepo = ElasticsearchPool.getRepoByCustomer("abc", ProductRepository.class); 
repository.findByIdentifier("xxx").getCategory(); 

यह कार्यावधि में भंडार बनाने के लिए संभव है,:

मैं है, ऐसा ही कुछ चाहते हैं?

धन्यवाद एक बहुत मार्सेल

उत्तर

2

हां। वसंत के साथ यह संभव है। लेकिन आपको Repository के बजाय ElasticsearchTemplate का उपयोग करना चाहिए।

उदाहरण के लिए। मेरे पास दो उत्पाद हैं। वे विभिन्न सूचकांक में संग्रहित हैं।

@Document(indexName = "product-a", type = "product") 
public class ProductA { 

    @Id 
    private String id; 

    private String name; 

    private int value; 

    //Getters and setters 
} 

@Document(indexName = "product-b", type = "product") 
public class ProductB { 

    @Id 
    private String id; 

    private String name; 

    //Getters and setters 

} 

मान लीजिए कि उनके पास एक ही प्रकार है, इसलिए उनके पास समान फ़ील्ड हैं। लेकिन यह जरूरी नहीं है। दो उत्पादों में पूरी तरह से अलग फ़ील्ड हो सकते हैं।

public interface ProductARepository extends ElasticsearchRepository<ProductA, String> { 
} 


public interface ProductBRepository 
    extends ElasticsearchRepository<ProductB, String> { 


} 

यह भी आवश्यक नहीं है:

मैं दो खजाने की है। केवल परीक्षण के लिए। तथ्य यह है कि ProductA "product-a" अनुक्रमणिका में संग्रहीत है और ProductB "product-b" अनुक्रमणिका में संग्रहीत है।

एक ही प्रकार के साथ दो (दस, दर्जन) सूचकांक कैसे पूछें?

बस इस

@Repository 
public class CustomProductRepositoryImpl { 

    @Autowired 
    private ElasticsearchTemplate elasticsearchTemplate; 

    public List<ProductA> findProductByName(String name) { 
     MatchQueryBuilder queryBuilder = QueryBuilders.matchPhrasePrefixQuery("name", name); 

     //You can query as many indices as you want 
     IndicesQueryBuilder builder = QueryBuilders.indicesQuery(queryBuilder, "product-a", "product-b"); 

     SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); 

     return elasticsearchTemplate.query(searchQuery, response -> { 
      SearchHits hits = response.getHits(); 
      List<ProductA> result = new ArrayList<>(); 
      Arrays.stream(hits.getHits()).forEach(h -> { 
       Map<String, Object> source = h.getSource(); 
       //get only id just for test 
       ProductA productA = new ProductA() 
         .setId(String.valueOf(source.getOrDefault("id", null))); 
       result.add(productA); 
      }); 
      return result; 
     }); 
    } 

} 

के रूप में आप जितने चाहें, उतने सूचकांक खोज सकते हैं की तरह कस्टम भंडार का निर्माण और आप पारदर्शी रूप से ProductARepositoryadding custom behavior to single repositories

दूसरा समाधान में इस व्यवहार इंजेक्षन कर सकते हैं indices aliases उपयोग करने के लिए है, लेकिन आप कस्टम मॉडल या कस्टम भंडार भी बनाना था।

संबंधित मुद्दे