การจัดกลุ่มผลการค้นหาโดยใช้ GroupingSearch
แพ็คเกจ org.apache.lucene.search.grouping คำอธิบาย
โมดูลนี้สามารถจัดกลุ่มผลการค้นหาของ Lucene และฟิลด์ที่มีค่าเดียวที่ระบุจะถูกรวบรวมเข้าด้วยกัน ตัวอย่างเช่นกลุ่มตามฟิลด์ "ผู้เขียน" เอกสารที่มีค่าฟิลด์ "ผู้เขียน" เดียวกันจะถูกแบ่งออกเป็นกลุ่ม
เมื่อจัดกลุ่มคุณต้องป้อนข้อมูลที่จำเป็นบางอย่าง:
1. Groupfield: กลุ่มตามสาขานี้ ตัวอย่างเช่นหากคุณใช้ฟิลด์ "ผู้แต่ง" ในการจัดกลุ่มหนังสือในแต่ละกลุ่มจะเป็นผู้เขียนคนเดียวกัน เอกสารที่ไม่มีโดเมนนี้จะถูกแบ่งออกเป็นกลุ่มแยกต่างหาก
2. กลุ่ม: การเรียงลำดับกลุ่ม
3. TopNgroups: มีกี่กลุ่มที่เก็บรักษาไว้ ตัวอย่างเช่น 10 หมายความว่ามีเพียง 10 กลุ่มแรกเท่านั้นที่ถูกเก็บรักษาไว้
4. GroupOffset: ค้นหากลุ่มกลุ่มที่ได้รับการจัดอันดับก่อน ตัวอย่างเช่น 3 หมายถึงการคืน 7 กลุ่ม (สมมติว่า opngroups เท่ากับ 10) มันมีประโยชน์มากในการปนเปื้อนเช่นมีเพียง 5 กลุ่มเท่านั้นที่แสดงต่อหน้า
5. WithingRoupsort: จัดเรียงเอกสารเป็นกลุ่ม หมายเหตุ: ความแตกต่างระหว่างที่นี่และกลุ่ม
6. Withgroupoffset: ค้นหาเอกสารที่จัดอันดับเป็นอันดับแรกในแต่ละกลุ่ม
การจัดกลุ่มผลการค้นหานั้นง่ายกว่าที่จะใช้ GroupingSearch
การแนะนำ DroupingSearch API เอกสารแนะนำ:
คลาสความสะดวกในการจัดกลุ่มในสภาพแวดล้อมที่ไม่กระจาย
การจัดกลุ่มในสภาพแวดล้อมที่ไม่แจกจ่าย
คำเตือน: API นี้เป็นการทดลองและอาจเปลี่ยนแปลงในรูปแบบที่เข้ากันไม่ได้ในรุ่นต่อไป
ใช้เวอร์ชัน 4.3.1 ที่นี่
วิธีสำคัญบางประการ:
รหัสตัวอย่าง:
1. ก่อนอื่นดูรหัสดัชนี
Class Public IndexHelper {เอกสารส่วนตัว; ไดเรกทอรีไดเรกทอรีส่วนตัว นักเขียนดัชนีดัชนีส่วนตัว ไดเรกทอรีสาธารณะ getDirectory () {directory = (ไดเรกทอรี == null)? ใหม่ Ramdirectory (): ไดเรกทอรี; ไดเรกทอรีคืน; } Private IndexWriterConfig getConfig () {ส่งคืน IndexWriterConfig ใหม่ (เวอร์ชัน Lucene_43, Ikanalyzer ใหม่ (จริง)); } Private IndexWriter getIndExWriter () {ลอง {ส่งคืน indexwriter ใหม่ (getDirectory (), getConfig ()); } catch (ioexception e) {e.printstacktrace (); คืนค่า null; }} public indexSearcher getIndexSearcher () พ่น IOException {ส่งคืนดัชนีใหม่ (DirectoryReader.Open (getDirectory ())); } / ** * สร้างดัชนีสำหรับการทดสอบกลุ่ม * @param ผู้แต่ง * @param เนื้อหา * / โมฆะสาธารณะ createindexforGroup (int id, สตริงผู้เขียน, เนื้อหาสตริง) {indexwriter = getIndExWriter (); เอกสาร = เอกสารใหม่ (); document.add (ใหม่ intfield ("id", id, field.store.yes)); document.add (ใหม่ Stringfield ("ผู้แต่ง", ผู้แต่ง, field.store.yes)); document.add (New TextField ("เนื้อหา", เนื้อหา, field.store.yes)); ลอง {indexwriter.adddocument (เอกสาร); Indexwriter.Commit (); indexwriter.close (); } catch (ioexception e) {e.printstacktrace (); -2. การจัดกลุ่ม:
กลุ่มสาธารณะ Grouptestpublic Void Group (IndexSearcher IndexSearcher, String Groupfield, เนื้อหาสตริง) พ่น IOException, ParseException {GroupSeSearch GroupSeSearch = New GroupSesearch (GroupField); GroupingSearch.setGroupSort (Sortfield.field.field_score)); GroupingSearch.SetFillSortfields (จริง); GroupingSearch.setCachingInmb (4.0, จริง); groupingsearch.setallgroups (จริง); //groupingsearch.setallgroupheads(true); GroupingSearch.setGroupDocslimit (10); QueryParser parser = new QueryParser (เวอร์ชัน Lucene_43, "เนื้อหา", Ikanalyzer ใหม่ (จริง)); Query Query = parser.parse (เนื้อหา); TopGroups <BytesRef> result = groupingSearch.search (indexSearcher, Query, 0, 1000); System.out.println ("การค้นหาฮิต:" + result.totalhitcount); System.out.println ("การจัดกลุ่มผลการค้นหา:" + result.groups.length); เอกสารเอกสาร; สำหรับ (GroupDocs <BytesRef> GroupDocs: result.groups) {system.out.println ("กลุ่ม:" + GroupDocs.groupValue.UTF8TOSTRING ()); System.out.println ("บันทึกในกลุ่ม:" + GroupDocs.totalhits); //system.out.println("groupdocs.scoredocs.length: " + GroupDocs.scoredocs.length); สำหรับ (Scoredoc Scoredoc: GroupDocs.Scoredocs) {system.out.println (indexsearcher.doc (scoredoc.doc)); -3. การทดสอบอย่างง่าย:
โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น IOException, parseexception {indexhelper indexhelper = new indexhelper (); indexhelper.createindexforgroup (1, "มันเทศ", "โอเพนซอร์สจีน"); indexhelper.createindexforgroup (2, "มันเทศ", "ชุมชนโอเพ่นซอร์ส"); indexhelper.createindexforgroup (3, "มันเทศ", "การออกแบบรหัส"); indexhelper.createindexforgroup (4, "มันเทศ", "การออกแบบ"); indexhelper.createIndexforGroup (5, "Jiexian", "การพัฒนา Lucene"); indexhelper.createindexforgroup (6, "Jiexian", "Lucene Practical Combat"); indexhelper.createindexforgroup (7, "Jiexian", "โอเพนซอร์ส Lucene"); indexhelper.createindexforgroup (8, "Jiexian", "Open Source Solr"); indexhelper.createindexforgroup (9, "Sanxian", "โอเพนซอร์ส Sanxian Lucene"); indexhelper.createindexforgroup (10, "Sanxian", "Sanxian Open Source Solr"); indexhelper.createindexforgroup (11, "Sanxian", "โอเพนซอร์ส"); grouptest grouptest = new grouptest (); grouptest.group (indexhelper.getindexsearcher (), "ผู้แต่ง", "โอเพนซอร์ส"); - 4. ผลการทดสอบ:
สองวิธีของการเพจ
ลูซีนมีสองวิธีในการเพจ:
1. แบ่งผลการค้นหาโดยตรง วิธีนี้สามารถใช้ได้เมื่อปริมาณข้อมูลค่อนข้างเล็ก การอ้างอิงหลักของรหัสเพจคือ:
Scoredoc [] sd = xxx; // query เริ่มบันทึกตำแหน่ง int เริ่มต้น = pageSize * (currentPage - 1); // การสืบค้นยกเลิกตำแหน่งบันทึกตำแหน่ง int end = math.min (เริ่มต้น+pagesize, sd.length); สำหรับ (int i = เริ่มต้น;
2. ใช้การค้นหา (... )
Lucene ให้วิธีการเกินพิกัดห้าวิธีที่สามารถใช้ได้ตามต้องการ
Scoredoc After: ลดจำนวน Scoredoc ทั้งหมดสำหรับผลการค้นหาล่าสุดโดย 1;
แบบสอบถามแบบสอบถาม: วิธีการสอบถาม
int n: จำนวนผลลัพธ์ที่ส่งคืนสำหรับแต่ละเคียวรีนั่นคือจำนวนทั้งหมดของผลลัพธ์ต่อหน้า
ตัวอย่างง่ายๆของการใช้:
// คุณสามารถใช้แผนที่เพื่อบันทึกแผนที่ผลลัพธ์การค้นหาที่จำเป็น <สตริงวัตถุ> resultmap = ใหม่ hashmap <string, object> (); Scoredoc After = null; Query Query = xxtopDocs td = search.searchafter (หลังจาก, การสืบค้น, ขนาด); // รับหมายเลข hit resultmap.put ("num", td.totalhits); Scoredoc [] sd = td.scoredocs; สำหรับ (Scoredoc Scoredoc: SD) {// การประมวลผลผลการค้นหาแบบคลาสสิก} // ผลการค้นหาจำนวนเงินจำนวนรวมจะลดลง 1 หลัง = sd [td.scoredocs.length - 1]; // บันทึกหลังจากการค้นหาครั้งต่อไปนั่นคือหน้าถัดไปเริ่มต้นผลลัพธ์ map.put ("หลังจาก", หลังจาก); Return ResultsMap;