จะจัดทำดัชนีรหัสประเทศของแบบสอบถามด้วย lucene ได้อย่างไร

ฉันกำลังสร้างดัชนีลูซีนสำหรับชื่อเมืองและรหัสประเทศ (ขึ้นอยู่กับกันและกัน) ฉันต้องการให้รหัสประเทศเป็นแบบค้นหาด้วยตัวพิมพ์เล็กและตรงทั้งหมด

ในตอนแรก ตอนนี้ฉันพยายามค้นหารหัสประเทศเดียวและค้นหาองค์ประกอบที่จัดทำดัชนีทั้งหมดที่ตรงกับรหัสนั้น โดยผลลัพธ์ของฉันว่างเปล่าเสมอ

//prepare
VERSION = Version.LUCENE_4_9;
IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer());

//index
Document doc = new Document();
doc.add(new StringField("countryCode", countryCode, Field.Store.YES));
writer.addDocument(doc);

//lookup
Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer()).parse(countryCode);

ผลลัพธ์: เมื่อฉันค้นหารหัส coutry เช่น "IT", "DE", "EN" ฯลฯ ผลลัพธ์จะว่างเปล่าเสมอ ทำไม SimpleAnalyzer มาจากรหัสประเทศ 2 ตัวอักษรใช่หรือไม่


person membersound    schedule 01.08.2014    source แหล่งที่มา


คำตอบ (2)


ฉันสับสนเล็กน้อยที่นี่ ฉันจะถือว่าตัวเขียนดัชนีของคุณเริ่มต้นได้ในบางส่วนของโค้ดของคุณที่ไม่ได้ระบุไว้ แต่คุณไม่กล้าส่ง Version ไปยัง SimpleAnalyzer เหรอ? ไม่มีตัวสร้าง arg สำหรับ SimpleAnalyzer ไม่ใช่ตั้งแต่ 3.X อยู่แล้ว

นั่นเป็นปัญหาจริงเดียวที่ฉันเห็น นี่คือตัวอย่างการทำงานโดยใช้โค้ดของคุณ:

private static Version VERSION;

public static void main(String[] args) throws IOException, ParseException {
    //prepare
    VERSION = Version.LUCENE_4_9;
    Directory dir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer(VERSION));
    IndexWriter writer = new IndexWriter(dir, config);

    String countryCode = "DE";

    //index
    Document doc = new Document();
    doc.add(new TextField("countryCode", countryCode, Field.Store.YES));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher search = new IndexSearcher(DirectoryReader.open(dir));
    //lookup
    Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer(VERSION)).parse(countryCode);

    TopDocs docs = search.search(query, 1);
    System.out.println(docs.totalHits);
}
person femtoRgon    schedule 02.08.2014
comment
ฉันพบปัญหา: ฉันใช้ StringField แทน TextField เนื่องจากเอกสารแจ้งว่า: ฟิลด์ที่มีการจัดทำดัชนีแต่ไม่ได้โทเค็น: ค่าสตริงทั้งหมดถูกจัดทำดัชนีเป็นโทเค็นเดียว ตัวอย่างเช่น อาจใช้สำหรับช่อง 'ประเทศ' หรือ 'id' คุณรู้ไหมว่าทำไมสิ่งนี้ถึงไม่ทำงาน? ใช้งานได้เมื่อใช้ TextField ตามที่คุณแนะนำ+ - person membersound; 04.08.2014
comment
ใช่ การแก้ไขนั้นอธิบายได้ StringField ไม่ได้รับการวิเคราะห์ การแสดงดัชนียังคงเป็นตัวพิมพ์ใหญ่ ข้อความค้นหาของคุณยังคงอยู่ในระหว่างการวิเคราะห์ ดังนั้น SimpleAnalyzer จะถูกลดขนาดลง - person femtoRgon; 04.08.2014

สำหรับ StringField คุณสามารถใช้ TermQuery แทน QueryParser

Directory dir = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, new SimpleAnalyzer(Version.LUCENE_4_9));
IndexWriter writer = new IndexWriter(dir, config);

String countryCode = "DE";

// index
Document doc = new Document();
doc.add(new StringField("countryCode", countryCode, Store.YES));
writer.addDocument(doc);
writer.close();

IndexSearcher search = new IndexSearcher(DirectoryReader.open(dir));
//lookup
Query query = new TermQuery(new Term("countryCode", countryCode));

TopDocs docs = search.search(query, 1);
System.out.println(docs.totalHits);
person serem    schedule 07.08.2014