SolrJ 常用增删查

阅读更多

Solr搜索引擎一般是只会进行增删查,对于Solr的数据结构,可以理解为类似MongoDB的类型的数据库。

与MonogoDB的Schema Free不同。

Solr还是需要自己定义字段,并且做一些设置的。

 

而Solr一般在修改索引的时候,会选择全量更新,所以一般不会有单条索引的更新操作。

 

SolrJ添加索引:

public class SolrJAdd {

	public static void main(String[] args) {
		String urlString = "http://localhost:8983/solr";
		SolrServer solr = new HttpSolrServer(urlString);
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", "552199");
		document.addField("name", "Gouda cheese wheel");
		document.addField("price", "49.99");
		document.addField("weight", 147);
		
		//add an array field 针对 multivalue字段
		String[] features = {"yellow","salty","hard"} ;
		document.addField("features", features);
		try {
			UpdateResponse response = solr.add(document);
			// Remember to commit your changes!
			solr.commit();
			System.out.println("commit!");
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

 

SolrJ删除索引

public class SolrJDelete {

	public static void main(String[] args) {
		String urlString = "http://localhost:8983/solr";
		SolrServer solr = new HttpSolrServer(urlString);
		
		 
		try {
			//删除查询到的索引信息
			solr.deleteByQuery("id:552199");
			solr.commit(true, true);
			
			System.out.println("delete success!");
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

SolrJ查询索引: 

public class SolrJSearcher {

	public static void main(String[] args) throws SolrServerException {
		String urlString = "http://localhost:8983/solr";
		HttpSolrServer solr = new HttpSolrServer(urlString);

		SolrQuery query = new SolrQuery();
		//添加查询
		query.setQuery("cat:music");
		
		//添加过滤条件
		query.addFilterQuery("price:[50 TO 400]");
		
		// 排序
		query.addSort("id", ORDER.asc);
        
		// 分页:start开始页,rows每页显示记录条数
		query.setStart(0);
		query.setRows(20);
        
		// 设置高亮
		query.setHighlight(true); // 开启高亮组件
		query.addHighlightField("name");// 高亮字段
		query.setHighlightSimplePre("");
        // 标记,高亮关键字前缀
		query.setHighlightSimplePost("");
		
		QueryResponse response = solr.query(query);
		SolrDocumentList docs = response.getResults();
		System.out.println("文档个数:" + docs.getNumFound());
		System.out.println("查询时间:" + response.getQTime());
		for (SolrDocument doc : docs) {
			System.out.println(doc.getFieldNames());
			System.out.println("id: " + doc.getFieldValue("id"));
			System.out.println("price: " + doc.getFieldValue("price"));
			System.out.println("name: " + doc.getFieldValue("name"));
			System.out.println();
		}
	}
}

 

SolrJ Facet查询:

public class SolrJFacetSearch {

	public static void main(String[] args) {
		String urlString = "http://localhost:8983/solr";
		SolrServer solr = new HttpSolrServer(urlString);
		
		SolrQuery query = new SolrQuery();
		//Facet为solr中的层次分类查询
        //分片信息
		query.setFacet(true)
            .setQuery("*:*")
            .setFacetMinCount(1)
            .setFacetLimit(5)//段
            //.setFacetPrefix("electronics", "cat")
            .setFacetPrefix("cor")//查询manu、name中关键字前缀是cor的
            .addFacetField("manu")
            .addFacetField("name");//分片字段
		
		
		try {
			QueryResponse response = solr.query(query);

			// 输出查询结果集
			SolrDocumentList list = response.getResults();
			System.out.println("Query result nums: " + list.getNumFound());

			for (int i = 0; i < list.size(); i++) {
				//SolrDocument
				System.out.println(list.get(i));
			}

			System.out.println("All facet filed result: ");
			// 输出分片信息
			List facets = response.getFacetFields();
			for (FacetField facet : facets) {
				System.out.println(facet);
				List facetCounts = facet.getValues();
				for (FacetField.Count count : facetCounts) {
					// 关键字 - 出现次数
					System.out.println(count.getName() + ": "
							+ count.getCount());
				}
			}

			System.out.println("Search facet [name] filed result: ");
			// 输出分片信息
			FacetField facetField = response.getFacetField("name");
			List facetFields = facetField.getValues();
			for (Count count : facetFields) {
				// 关键字 - 出现次数
				System.out.println(count.getName() + ": " + count.getCount());
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	}
}

 

 Refer to:http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html

 

你可能感兴趣的:(solrj,example,例子)