使用Solrのクエリテスト

com.offcn.core.testをパッケージ化。

輸入com.offcn.core.pojo.Item;
輸入org.junit.Test;
輸入org.junit.runner.RunWith;
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.data.solr.core.SolrTemplate;
輸入org.springframework.data.solr.core.query.Criteria;
輸入org.springframework.data.solr.core.query.SimpleQuery;
輸入org.springframework.data.solr.core.query.result.ScoredPage;
輸入org.springframework.test.context.ContextConfiguration。
輸入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

インポートのjava.math.BigDecimal;
輸入はjava.util.ArrayList;
輸入はjava.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(「クラスパス:ApplicationContextの-solr.xml」)
パブリッククラスItemTest {

@Autowired
private SolrTemplate solrTemplate;

@Test
public void testIndexCreatAndUpdate(){
    List<Item> itemList = new ArrayList<Item>();
    for(long i=1;i<100;i++){
        Item item = new Item();
        item.setId(i);
        item.setTitle("苹果手机"+i);
        item.setCategory("手机");
        item.setPrice(new BigDecimal("123"));
        item.setBrand("苹果");
        itemList.add(item);
    }
    solrTemplate.saveBeans(itemList);
    solrTemplate.commit();
}

@Test
public  void testItem(){
    SimpleQuery query = new SimpleQuery();
    Criteria criteria = new Criteria("item_brand").contains("苹果");

    query.addCriteria(criteria);
    ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
    int i = items.getTotalPages();
    System.out.println(i+"===========================");

}

}

公開された33元の記事 ウォンの賞賛0 ビュー861

おすすめ

転載: blog.csdn.net/ninth_spring/article/details/103721255