Solr 시작 (1)

9288 단어
하나.솔로가 뭐예요?
엔터프라이즈급 애플리케이션 검색 서버.
2.솔로 사용
    1.가이드 팩

		
		
			junit
			junit
			4.11
		
		
			org.apache.solr
			solr-solrj
			4.10.2
		
		
		
			org.slf4j
			slf4j-log4j12
			1.7.22
		
		
			commons-logging
			commons-logging
			1.2
		
	
	
		
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.2
				
					1.8
					1.8
					UTF-8
				
			
		
	

    2.인덱스 라이브러리 데이터 추가
/**
	 *  
	 */
	@Test
	public void testName() throws Exception {
		//   solr 
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		
		//   
		SolrInputDocument solrInputDocument = new SolrInputDocument();
		solrInputDocument.addField("id", "6");
		solrInputDocument.addField("title", "oppo , , , ");
		//   
		solrServer.add(solrInputDocument);
		solrServer.commit();
	}

    3.여러 인덱스 라이브러리 데이터 추가
/**
	 *  
	 */
	@Test
	public void testName001() throws Exception {
		//   solr 
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		
		//   
		List list =new ArrayList();
		
		SolrInputDocument solrInputDocument = new SolrInputDocument();
		solrInputDocument.addField("id", "1");
		solrInputDocument.addField("title", " , ");
		
		SolrInputDocument solrInputDocument2 = new SolrInputDocument();
		solrInputDocument2.addField("id", "2");
		solrInputDocument2.addField("title", " , ");
		
		SolrInputDocument solrInputDocument3 = new SolrInputDocument();
		solrInputDocument3.addField("id", "3");
		solrInputDocument3.addField("title", " , ");
		
		SolrInputDocument solrInputDocument4 = new SolrInputDocument();
		solrInputDocument4.addField("id", "4");
		solrInputDocument4.addField("title", " , ");
		list.add(solrInputDocument4);
		list.add(solrInputDocument2);
		list.add(solrInputDocument3);
		list.add(solrInputDocument);
	
		//   
		solrServer.add(list);

	
	
		solrServer.commit();
		
	}

    4.인덱스 라이브러리 데이터에javabean 대상 추가
4.1 Javabean 객체 설정: 메모 @Field를 추가하지 않으면 오류가 발생합니다.
주해가 틀린 정보를 추가하지 않습니다 org.apache.solr.client.solrj.beans.BindingException: class: class
public class Item {
	@Field
	private String id;
	@Field
	private String title;
	@Field
	private String text;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	@Override
	public String toString() {
		return "Item [id=" + id + ", title=" + title + ", text=" + text + "]";
	}
	
	

}

4.2 테스트 사례
    
	/**
	 *  javabean 
	 * 
	 */
	@Test
	public void testName2() throws Exception {
		//   
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		//   
		Item item = new Item();
		item.setId("7");
		item.setTitle(" , ");
		//   
		solrServer.addBean(item);
		solrServer.commit();
		
	}

    5.id에 따라 인덱스 데이터베이스 삭제
/**
	 *  id 
	 */
	@Test
	public void testName3() throws Exception {
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		solrServer.deleteById("6");
		solrServer.commit();
	}

    6.인덱스 라이브러리 태그에 따라 삭제
	/**
	 *  
	 */
	@Test
	public void testName4() throws Exception {
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		String query="title: ";
		solrServer.deleteByQuery(query);
		solrServer.commit();
	}

    7.모든 데이터 조회
/**
	 *  
	 */
	@Test
	public void testName5() throws Exception {
		//    
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		//   
		SolrQuery solrQuery = new SolrQuery("*:*");
		// , , 
		QueryResponse response = solrServer.query(solrQuery);
		SolrDocumentList documentList = response.getResults();
		System.out.println(" "+documentList.size()+" ");
		for (SolrDocument solrDocument : documentList) {
			Object fieldValue = solrDocument.getFieldValue("id");
			System.out.println("id============>"+fieldValue);
			Object fieldValue2 = solrDocument.getFieldValue("title");
			System.out.println("title==============>"+fieldValue2);
		}
	}

    8.조건 포함 모든 데이터 조회
/**
	 *  
	 */
	@Test
	public void testName6() throws Exception {
		//   
		HttpSolrServer solrServer = new  HttpSolrServer("http://localhost:8080/solr/core2");
		//   
		SolrQuery solrQuery = new SolrQuery("title: ");
		//     
		QueryResponse response = solrServer.query(solrQuery);
		//   
		SolrDocumentList documentList = response.getResults();
		System.out.println(" "+documentList.size()+" ");
		for (SolrDocument solrDocument : documentList) {
			System.out.println("id==========>"+solrDocument.getFieldValue("id"));
			System.out.println("title=======>"+solrDocument.getFieldValue("title"));
			System.out.println("title=======>"+solrDocument.getFieldValue("name"));
			System.out.println("title=======>"+solrDocument.getFieldValue("text"));
			
			
		}
		
	}

    9.조회된 데이터는javabean으로 되돌아온다
/**
	 *  javabean 
	 */
	@Test
	public void testName7() throws Exception {
		// . 
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		// , 
		SolrQuery solrQuery = new SolrQuery("*:*");
		
		// . 
		QueryResponse response = solrServer.query(solrQuery);
		// , 
		List list = response.getBeans(Item.class);
		System.out.println(" :"+list.size());
		for (Item item : list) {
			System.out.println("Id:"+item.getId());
			System.out.println("Title:"+item.getTitle());
			
			
		}
		
	}

10. 질의 결과, SolrQuery 정렬
	/**
	 *  、SolrQuery 
	 */
	@Test
	public void testName8() throws Exception {
		//1  
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		//2  
		SolrQuery solrQuery = new SolrQuery("*:*");
		solrQuery.setSort("id", ORDER.asc);
		//3  
		QueryResponse response = solrServer.query(solrQuery);
		//4  
		List beans = response.getBeans(Item.class);
		System.out.println(" :"+beans.size());
		for (Item item : beans) {
			System.out.println("Id:"+item.getId());
			System.out.println("Title:"+item.getTitle());
		}
	}

    11.검색 결과 페이지 나누기
	/**
	 *  
	 */
	@Test
	public void testName9() throws Exception {
		//  
		int pageNum = 2; // 
		int pageSize = 3; // 
		
		//1  
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		//2  
		SolrQuery query = new SolrQuery("title:*");
		query.setSort("id", ORDER.asc);
		query.setStart((pageNum-1)*pageSize);
		query.setRows(pageSize);
		//3  
		QueryResponse response = solrServer.query(query);
		
		//4  
		List beans = response.getBeans(Item.class);
		
		System.out.println(" :"+beans.size());
		for (Item item : beans) {
			System.out.println("Id:"+item.getId());
			System.out.println("Title:"+item.getTitle());
		}
	}

    12.질의 결과 강조 표시
/**
	 *   SolrQuery 
	 */
	@Test
	public void testName10() throws Exception {
		//1  
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
		//2  
		SolrQuery query = new SolrQuery("title:oppo");
		query.setHighlightSimplePre("");
		query.setHighlightSimplePost("");
		query.addHighlightField("title");
		//3  
		QueryResponse response = solrServer.query(query);
		//4  
		List items  = response.getBeans(Item.class);
		
		
		//5  
		Map>> highlighting  = response.getHighlighting();
		
		
		System.out.println(" :"+items.size());
		for (Item item : items) {
			System.out.println("Id:"+item.getId());
			System.out.println("Title:"+highlighting.get(item.getId()).get("title").get(0) );
			
		}
		
	}

좋은 웹페이지 즐겨찾기