Не удалось прочитать данные индекса Lucene с использованием Java - PullRequest
0 голосов
/ 11 ноября 2019

Test.java (для поиска в индексе)

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;


public class Test {
    private static final String INDEX_DIR = "C:\\DEV\\index";
    private static final StandardAnalyzer analyzer = new StandardAnalyzer();
    public static void main(String[] args) {
        IndexSearcher searcher;
        try {
            searcher = createSearcher();
            TopDocs foundDocs;
            try {
                foundDocs = searchByRawData("<DEBUG>", searcher);
                System.out.println("Total Results : " + foundDocs.totalHits);

                for (ScoreDoc sd : foundDocs.scoreDocs) {
                    Document doc = searcher.doc(sd.doc);
                    System.out.println(doc.getField("RawData"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    private static TopDocs searchByRawData(String RawData, IndexSearcher searcher) throws Exception {
        QueryParser qp = new QueryParser("RawData", analyzer);
        Query search = qp.parse(RawData);
        TopDocs hits = searcher.search(search, 1000);
        return hits;
    }

    private static IndexSearcher createSearcher() throws IOException {
        Directory dir = FSDirectory.open(Paths.get(INDEX_DIR));
        DirectoryReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
        return searcher;
    }
}

OutData.java (для извлечения данных из БД и индексации с использованием Lucene)

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;
import java.sql.*;
import java.time.Duration;

public class OutData {
    private static final String CONN = "jdbc:mysql://localhost:3306/db_name";
    private static final String UNAME = "***";
    private static final String PW = "***";
    private static final String QUE = "SELECT * from test";
    private static final Logger log = Logger.getLogger(OutData.class);

    public static void main(String[] args) throws SQLException {
        wirelogOutData();
    }

    private static void wirelogOutData() throws SQLException {
        BasicConfigurator.configure();
        extractRaw();

    }

    private static void extractRaw() {
        BasicConfigurator.configure();
        Directory index = null;
        try {
            index = FSDirectory.open(Paths.get("C:\\DEV\\index"));
            StandardAnalyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter writer = new IndexWriter(index, indexWriterConfig);
            log.info("Optimizing...");
            indexDocs(writer);
            log.info("Done");
            writer.close();
        } catch (IOException e) {
            log.error(e);
        }
    }

    static void indexDocs(IndexWriter writer) {
        try (Connection con = DriverManager.getConnection(CONN, UNAME, PW);
                PreparedStatement ps = con.prepareStatement(QUE);) {
            try (ResultSet rs = con.createStatement().executeQuery(QUE)) {
                while (rs.next()) {
                    Document doc = new Document();
                    doc.add(new StringField("RawData", rs.getString("RawData"), Field.Store.YES));
                    writer.addDocument(doc);
                }
            }
        } catch (Exception e) {
            log.error(e);
        }
    }
}

bla.log

<INFO > [BalanceInquirySCImpl] 30-05-2019 00:00:24: Time started = 2019-05-30 00:00:24.156
<DEBUG> [BalanceInquirySMImpl] 30-05-2019 00:00:24: getByAccountNoReportList CUSTOM, start!
<DEBUG> [PGatewayMiddlewareServiceImpl] 30-05-2019 00:00:24: Execute PGateway, template:gcm.middleware.mapping.balanceinquiry.multi,service:getBalanceInquiryMulti
<INFO > [PGatewayMiddlewareServiceImpl] 30-05-2019 00:00:24: [getBalanceInquiryMulti] - Elapsed Time : 0.098 s
<DEBUG> [BalanceInquirySMImpl] 30-05-2019 00:00:24: levelIdMap == {}
<DEBUG> [ActivityLogConfigurer] 30-05-2019 00:00:24: Menu Code : MNU_GCME_040100
<INFO > [BalanceInquirySCImpl] 30-05-2019 00:00:24: approverID in balance inquiry getByAcctNoAndAcctGroupIdList approverBy -----------------------  2c9dc20f4d96600a014da31a96ff7b2e
<INFO > [BalanceInquirySCImpl] 30-05-2019 00:00:24: approverID in balance inquiry getByAcctNoAndAcctGroupIdList fromLogin -----------------------  Web
<DEBUG> [PredefinedBeneficiaryAction] 30-05-2019 00:00:33: checkedList = 0,0,0,0,0,1,0,0,0,0

Я не могу найти данные Lucene, которые я уже проиндексировал из БД. Так что в моей БД есть только 2 столбца с именами RawData и Source. И я просто извлекаю rawData в данные Lucene. RawData в моей БД похож на bla.log, а Source я установил в ноль. Я ставлю каждую строку на 1 строку в БД. когда я хочу читать и искать в данных lucene, он не может обнаружить / прочитать. я не знаю, что не так с моим кодом. Любая помощь? спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...