package com.easyagents.search.engine.lucene;

import com.easyagents.core.document.Document;
import com.easyagents.search.engine.service.KeywordSearchMetadataKeys;
import com.easyagents.search.engine.service.KeywordSearchRequest;
import org.junit.Assert;
import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class LuceneSearcherTest {

    @Test
    public void shouldFilterByKnowledgeIdAndSearchTitleAndContent() throws Exception {
        Path tempDir = Files.createTempDirectory("lucene-searcher-test");
        LuceneConfig config = new LuceneConfig();
        config.setIndexDirPath(tempDir.toString());
        LuceneSearcher searcher = new LuceneSearcher(config);

        Document first = new Document();
        first.setId("1");
        first.setTitle("客服标题");
        first.setContent("这里没有关键字");
        first.addMetadata(KeywordSearchMetadataKeys.KNOWLEDGE_ID, "100");

        Document second = new Document();
        second.setId("2");
        second.setTitle("别的知识库");
        second.setContent("客服内容");
        second.addMetadata(KeywordSearchMetadataKeys.KNOWLEDGE_ID, "200");

        Assert.assertTrue(searcher.addDocument(first));
        Assert.assertTrue(searcher.addDocument(second));

        KeywordSearchRequest request = KeywordSearchRequest.of("客服", 10);
        request.setKnowledgeId("100");
        List<Document> results = searcher.searchDocuments(request);

        Assert.assertEquals(1, results.size());
        Assert.assertEquals("1", String.valueOf(results.get(0).getId()));
        Assert.assertEquals("100", String.valueOf(results.get(0).getMetadata(KeywordSearchMetadataKeys.KNOWLEDGE_ID)));
    }
}
