日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

11.SolrJ索引操作

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 11.SolrJ索引操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建索引

說明:根據id(唯一約束)域來更新Document的內容,如果根據id值搜索不到id域則會執行添加操作,如果找到則更新。

  • public void testCreateIndex() throws SolrServerException, IOException {
  • SolrServer solrServer = new HttpSolrServer(urlString);
  • SolrInputDocument document = new SolrInputDocument();
  • document.addField("id", "c0001");
  • document.addField("product_name", "傳智java教程");//商品名稱
  • document.addField("product_price", 86.5f);//商品價格
  • document.addField("product_picture", "382782828.jpg");//商品圖片
  • document.addField("product_description", "這是一本深入淺出講解java技術的書籍!");//商品描述
  • document.addField("product_catalog_name", "javabook");//商品分類
  • UpdateResponse response = solrServer.add(document);
  • // 提交
  • solrServer.commit();
  • }
  • 刪除索引

  • public void testDeleteIndex() throws SolrServerException, IOException {
  • SolrServer solrServer = new HttpSolrServer(urlString);
  • //根據id刪除
  • UpdateResponse response = solrServer.deleteById("c0001");
  • //根據多個id刪除
  • // solrServer.deleteById(ids...);
  • //自動查詢條件刪除
  • // solrServer.deleteByQuery("product_keywords:教程");
  • // 提交
  • solrServer.commit();
  • }
  • 搜索索引

    簡單搜索

  • public void testSearch() throws SolrServerException {
  • SolrServer solr = new HttpSolrServer(urlString);
  • // 查詢對象
  • SolrQuery query = new SolrQuery();
  • //設置查詢條件,名稱“q”是固定的且必須 的
  • //搜索product_keywords域,product_keywords是復制域包括product_name和product_description
  • query.set("q", "product_keywords:java教程");
  • // 請求查詢
  • QueryResponse response = solr.query(query);
  • // 查詢結果
  • SolrDocumentList docs = response.getResults();
  • // 查詢文檔總數
  • System.out.println("查詢文檔總數" + docs.getNumFound());
  • for (SolrDocument doc : docs) {
  • //商品主鍵
  • String id = (String) doc.getFieldValue("id");
  • //商品名稱
  • String product_name = (String) doc.getFieldValue("product_name");
  • //商品價格
  • Float product_price = (Float) doc.getFieldValue("product_price");
  • //商品圖片
  • String product_picture = (String) doc.getFieldValue("product_picture");
  • //商品分類
  • String product_catalog_name = (String) doc.getFieldValue("product_catalog_name");
  • System.out.println("=============================");
  • System.out.println(id);
  • System.out.println(product_name);
  • System.out.println(product_price);
  • System.out.println(product_picture);
  • System.out.println(product_catalog_name);
  • }
  • }
  • 組合查詢

  • public void testSearch2() throws SolrServerException {
  • SolrServer solr = new HttpSolrServer(urlString);
  • // 查詢對象
  • SolrQuery query = new SolrQuery();
  • // 搜索product_keywords域,product_keywords是復制域包括product_name和product_description
  • // 設置商品分類、關鍵字查詢
  • // query.set("q", "product_keywords:掛鉤 AND product_catalog_name:幽默雜貨");
  • query.setQuery("product_keywords:掛鉤 AND product_catalog_name:幽默雜貨");
  • // 設置價格范圍
  • query.set("fq", "product_price:[1 TO 20]");
  • // 查詢結果按照價格降序排序
  • // query.set("sort", "product_price desc");
  • query.addSort("product_price", ORDER.desc);
  • // 請求查詢
  • QueryResponse response = solr.query(query);
  • // 查詢結果
  • SolrDocumentList docs = response.getResults();
  • // 查詢文檔總數
  • System.out.println("查詢文檔總數" + docs.getNumFound());
  • for (SolrDocument doc : docs) {
  • // 商品主鍵
  • String id = (String) doc.getFieldValue("id");
  • // 商品名稱
  • String product_name = (String) doc.getFieldValue("product_name");
  • // 商品價格
  • Float product_price = (Float) doc.getFieldValue("product_price");
  • // 商品圖片
  • String product_picture = (String) doc.getFieldValue("product_picture");
  • // 商品分類
  • String product_catalog_name = (String) doc.getFieldValue("product_catalog_name");
  • System.out.println("=============================");
  • System.out.println("id=" + id);
  • System.out.println("product_name=" + product_name);
  • System.out.println("product_price=" + product_price);
  • System.out.println("product_picture=" + product_picture);
  • System.out.println("product_catalog_name=" + product_catalog_name);
  • }
  • }
  • 分頁、高亮

  • public void testSearch3() throws SolrServerException {
  • SolrServer solr = new HttpSolrServer(urlString);
  • // 查詢對象
  • SolrQuery query = new SolrQuery();
  • // 設置商品分類、關鍵字查詢
  • query.setQuery("product_keywords:透明掛鉤 ");
  • // 分頁參數
  • // 每頁顯示記錄數
  • int pageSize = 2;
  • // 當前頁碼
  • int curPage = 2;
  • // 開始記錄下標
  • int begin = pageSize * (curPage - 1);
  • // 起始下標
  • query.setStart(begin);
  • // 結束下標
  • query.setRows(pageSize);
  • // 設置高亮參數
  • query.setHighlight(true); // 開啟高亮組件
  • query.addHighlightField("product_name");// 高亮字段
  • query.setHighlightSimplePre("<span color='red'>");// 前綴標記
  • query.setHighlightSimplePost("</span>");// 后綴標記
  • // 請求查詢
  • QueryResponse response = solr.query(query);
  • // 查詢結果
  • SolrDocumentList docs = response.getResults();
  • // 查詢文檔總數
  • System.out.println("查詢文檔總數" + docs.getNumFound());
  • for (SolrDocument doc : docs) {
  • // 商品主鍵
  • String id = (String) doc.getFieldValue("id");
  • // 商品名稱
  • String product_name = (String) doc.getFieldValue("product_name");
  • // 商品價格
  • Float product_price = (Float) doc.getFieldValue("product_price");
  • // 商品圖片
  • String product_picture = (String) doc.getFieldValue("product_picture");
  • // 商品分類
  • String product_catalog_name = (String) doc.getFieldValue("product_catalog_name");
  • System.out.println("=============================");
  • System.out.println("id=" + id);
  • System.out.println("product_name=" + product_name);
  • System.out.println("product_price=" + product_price);
  • System.out.println("product_picture=" + product_picture);
  • System.out.println("product_catalog_name=" + product_catalog_name);
  • // 高亮信息
  • if (response.getHighlighting() != null) {
  • if (response.getHighlighting().get(id) != null) {
  • Map<String, List<String>> map = response.getHighlighting().get(id);// 取出高亮片段
  • if (map.get("product_name") != null) {
  • for (String s : map.get("product_name")) {
  • System.out.println(s);
  • }
  • }
  • }
  • }
  • }
  • }
  • 轉載于:https://www.cnblogs.com/wesly186/p/115607faa5f3f1bb68b4c61385c3c972.html

    總結

    以上是生活随笔為你收集整理的11.SolrJ索引操作的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。