根据上一节Solr学习之路–1环境集成搭建后,创建并查询了索引,可以看下统计信息:
添加索引
我们可以利用管理用户界面添加索引文档:
其他方式,在搭建环境时体现过。
删除索引
我们可以通过命令的方式或者通过HTTP,提交请求和管理界面的方式进行测试
通过id删除
java -Ddata=args -Dc=wenqy -jar post.jar "<delete><id>20160928</id></delete>"
通过查询条件删除
java -Ddata=args -Dc=wenqy -jar post.jar "<delete><query>name:小温之家</query></delete>"
也可以手动提交,期间可以可以查询出数据
java -Ddata=args -Dcommit=false -Dc=wenqy -jar post.jar "<delete><id>20160928</id></delete>"
然后提交事务,彻底删除
java -Dc=wenqy -jar post.jar -
提交请求的方式
删除所有数据
http://localhost:8983/solr/wenqy/update?stream.body=*:*&commit=true
根据查询条件删除
http://localhost:8983/solr/wenqy/update?stream.body=<delete><query>name:小温 AND cat:book</query></delete>&commit=true
通过用户管理界面删除,删除id为“201610091”的数据
SolrJ测试
添加索引
利用solr对java的支持,添加索引集合
- /**
- * 添加索引集合
- */
- public void addDocs() {
- Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
- for (int i=10; i<20; i++) {
- SolrInputDocument doc = new SolrInputDocument();
- doc.addField(“id”, “20161008”+i);
- doc.addField(“cat”, “book”);
- doc.addField(“name”, “小温之家-solr学习之路”+i);
- doc.addField(“price”, 9.99f);
- doc.addField(“author”, “wenqy”);
- doc.addField(“inStock”, true);
- docs.add(doc);
- }
- try {
- System.out.println(solrClient.add(docs));
- System.out.println(solrClient.commit());
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
以对象的方式添加索引集合
- /**
- * 以对象方式添加索引集合
- */
- public void addBeans() {
- List<BookIndex> indexs = new ArrayList<BookIndex>();
- for (int i=20; i<30; i++) {
- BookIndex index = new BookIndex();
- index.setId(“20161008”+i);
- index.setName(“小温之家-solr学习之路”+i);
- index.setCat(“book”);
- index.setPrice(6.66f);
- index.setAuthor(“wenqy2”);
- index.setInStock(true);
- indexs.add(index);
- }
- try {
- System.out.println(solrClient.addBeans(indexs));
- System.out.println(solrClient.commit());
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
删除索引
- /**
- * 删除索引
- */
- public void remove() {
- try {
- //删除id为20160928的索引
- solrClient.deleteById(“20160928”);
- solrClient.commit();
- //根据id集合,删除多个索引
- List<String> ids = new ArrayList<String>();
- ids.add(“201610080”);
- ids.add(“201610081”);
- solrClient.deleteById(ids);
- solrClient.commit(true, true);
- //删除查询到的索引信息
- solrClient.deleteByQuery(“id:201610082 id:201610083”);
- solrClient.commit(true, true);
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
查询索引
- /**
- * 以查询参数的方式查询
- * @param query
- */
- public void query(String query) {
- SolrParams params = new SolrQuery(query);
- try {
- QueryResponse response = solrClient.query(params);
- for (SolrDocument result :response.getResults()) {
- System.out.println(result);
- }
- } catch (SolrServerException | IOException e) {
- e.printStackTrace();
- }
- }
查询所有索引
- /**
- * 查询所有索引
- */
- public void queryAll() {
- ModifiableSolrParams params = new ModifiableSolrParams();
- // 查询关键词,*:*代表所有属性、所有值,即所有index
- params.set(“q”, “*:*”);
- // 分页,start=0就是从0开始,rows=10当前返回10条记录,下一页变化start这个值为10。
- params.set(“start”, 0);
- params.set(“rows”, Integer.MAX_VALUE);
- // 排序
- params.set(“sort”, “id desc”);
- // 返回字段
- params.set(“fl”, “*”);
- QueryResponse query;
- try {
- query = solrClient.query(params);
- for (SolrDocument result :query.getResults()) {
- System.out.println(result);
- }
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
查询基本用法
- /**
- * 测试案例:查询基本用法
- */
- public void queryCase() {
- //AND 并且
- SolrQuery params = new SolrQuery(“name:小温之家 AND cat:book”);
- //OR 或者
- // params.setQuery(“name:小温之家 AND author:wenqy”);
- //空格 等同于 OR
- // params.setQuery(“name:小温之家 author:wenqy”);
- //- 在要排除的词前加上- 号,即排除author为wenqy的数据
- params.setQuery(“name:小温之家 – author:wenqy”);
- //+ 必须包含”+” 号后面的数据
- // params.setQuery(“name:A + author:wenqy”);
- //查询name包含solr 小温之家
- params.setQuery(“name:solr,小温之家”);
- //author不包含wenqy
- params.setQuery(“name:solr,小温之家 NOT author:wenqy”);
- //50 <= price <= 200
- params.setQuery(“price:[50 TO 200]”);
- params.setQuery(“price:[5 TO 6]”);
- //params.setQuery(“price:[50 TO 200] – price:[5 TO 6]”);
- //params.setQuery(“price:[50 TO 200] + price:[5 TO 6]”);
- //50 <= price <= 200 AND 5 <= price <= 6
- params.setQuery(“price:[50 TO 200] AND price:[5 TO 6]”);
- //过滤器查询,可以提高性能 filter 类似多个条件组合,如and
- //params.addFilterQuery(“id:201609”);
- //params.addFilterQuery(“price:[50 TO 200]”);
- //params.addFilterQuery(“author:*”);
- //0 < price < 6 没有等于
- //params.addFilterQuery(“price:{0 TO 6}”);
- //排序
- params.addSort(“id”, ORDER.asc);
- //分页:start开始页,rows每页显示记录条数
- //params.add(“start”, “0”);
- //params.add(“rows”, “200”);
- //params.setStart(0);
- //params.setRows(200);
- //设置高亮
- params.setHighlight(true); // 开启高亮组件
- params.addHighlightField(“name”);// 高亮字段
- params.setHighlightSimplePre(“<span class=’red’>”);//标记,高亮关键字前缀
- params.setHighlightSimplePost(“</span>”);//后缀
- params.setHighlightSnippets(1);//结果分片数,默认为1
- params.setHighlightFragsize(1000);//每个分片的最大长度,默认为100
- //分片信息
- params.setFacet(true)
- .setFacetMinCount(1)
- .setFacetLimit(5)//段
- .addFacetField(“name”)//分片字段
- .addFacetField(“inStock”);
- //params.setQueryType(“”);
- try {
- QueryResponse response = solrClient.query(params);
- /*List<BookIndex> indexs = response.getBeans(BookIndex.class);
- for (int i = 0; i < indexs.size(); i++) {
- fail(indexs.get(i));
- }*/
- //输出查询结果集
- for (SolrDocument result :response.getResults()) {
- System.out.println(result);
- }
- //输出分片信息
- List<FacetField> facets = response.getFacetFields();
- for (FacetField facet : facets) {
- List<Count> facetCounts = facet.getValues();
- for (FacetField.Count count : facetCounts) {
- System.out.println(count.getName() + “: “ + count.getCount());
- }
- }
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
分片查询
- /**
- * 分片查询, 统计关键字及出现次数、或自动补全提示
- */
- public void facetQuery() {
- SolrQuery params = new SolrQuery(“*:*”);
- //排序
- params.addSort(“id”, ORDER.asc);
- // 分页
- params.setStart(0);
- params.setRows(200);
- //Facet为solr中的层次分类查询
- //分片信息
- params.setFacet(true)
- .setQuery(“name:小温”)
- .setFacetMinCount(1)
- .setFacetLimit(10)//段
- //.setFacetPrefix(“electronics”, “cat”)
- .setFacetPrefix(“wenqy”)//查询name中关键字前缀是小温之家
- .addFacetField(“author”);
- //.addFacetField(“name”);//分片字段
- try {
- QueryResponse response = solrClient.query(params);
- //输出查询结果集
- for (SolrDocument result :response.getResults()) {
- System.out.println(result);
- }
- //输出分片信息
- List<FacetField> facets = response.getFacetFields();
- for (FacetField facet : facets) {
- List<Count> facetCounts = facet.getValues();
- StringBuffer stringBuffer = new StringBuffer();
- for (FacetField.Count count : facetCounts) {
- stringBuffer.append(count.getName()+” “);
- }
- // 获取自动代码提示数组
- String[] split = stringBuffer.toString().split(” “);
- for (String s : split) {
- System.out.println(s);
- }
- }
- //输出分片信息
- FacetField facetField = response.getFacetField(“author”);
- List<Count> facetFields = facetField.getValues();
- for (Count count : facetFields) {
- //关键字 – 出现次数
- System.out.println(count.getName() + “: “ + count.getCount());
- }
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
实体绑定
文档与实体Bean的相互绑定
- /**
- * 文档与实体Bean的相互绑定
- */
- public void doc2Bean() {
- SolrDocument doc = new SolrDocument();
- doc.addField(“id”, “20161009”);
- doc.addField(“cat”, “book”);
- doc.addField(“name”, “小温之家-solr学习之路”);
- doc.addField(“price”, 9.99f);
- doc.addField(“author”, “wenqy3”);
- doc.addField(“inStock”, true);
- BookIndex index = new BookIndex();
- index.setId(“201610090”);
- index.setName(“小温之家-solr学习之路”);
- index.setCat(“book”);
- index.setPrice(6.66f);
- index.setAuthor(“wenqy3”);
- index.setInStock(true);
- DocumentObjectBinder binder = new DocumentObjectBinder();
- // BookIndex ->> SolrInputDocument
- System.out.println(binder.toSolrInputDocument(index));
- // SolrDocument ->> BookIndex
- System.out.println(binder.getBean(BookIndex.class, doc));
- SolrDocumentList list = new SolrDocumentList();
- list.add(doc);
- //SolrDocumentList ->> List
- System.out.println(binder.getBeans(BookIndex.class, list));
- }
实体Bean
- /**
- * Entity:book书籍,参考exampledocs的book.csv
- * 通过注解 将属性参与到index中
- * @author wenqy
- *
- */
- public class BookIndex {
- private String id;
- @Field
- private String name;
- @Field
- private String cat;
- @Field
- private float price;
- @Field
- private String author;
- @Field
- private boolean inStock;
- public String getId() {
- return id;
- }
- @Field
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getCat() {
- return cat;
- }
- public void setCat(String cat) {
- this.cat = cat;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public boolean isInStock() {
- return inStock;
- }
- public void setInStock(boolean inStock) {
- this.inStock = inStock;
- }
- public String toString() {
- return “{id:”+id+“,name:”+name+“,cat:”+cat+“,price:”+price+“,author:”+author+“,inStock:”+inStock+“}”;
- }
- }
代码调用测试
- public static void main(String[] args) {
- AddCoreTest search = new AddCoreTest();
- search.init();
- //search.addData();
- //search.addBeans();
- //search.addDocs();
- //search.remove();
- //search.queryAll();
- //search.query(“name:小温之家”);
- //search.queryCase();
- //search.facetQuery();
- search.doc2Bean();
- }
暂且这样吧,Solr的查询语法和复杂的查询功能有待挖掘。。。
发表评论