ApacheLucene。如何在PhraseQuery中将通配符应用于单词

时间:2018-07-18 15:55:59

标签: java apache lucene

如何在PhraseQuery中将通配符应用于单词。 我找到了类MultiPhraseQuery,但不了解它是如何工作的)我是新手 现在我的搜索看起来像“ Hi world”,应该是“ Hi * world *”

   public List<Long> search(String searchText){ 
    List<Long> ids = new ArrayList<>();
    String indexDir = "index";

    IndexReader reader = null;
    try {
        Directory directory = FSDirectory.open(Paths.get(indexDir));
        reader = DirectoryReader.open(directory);
    } catch (IOException e){
        e.toString();
    }
    IndexSearcher searcher = new IndexSearcher(reader);
    StandardAnalyzer standardAnalyzer = new StandardAnalyzer();

    PhraseQuery.Builder phraseQuery = new PhraseQuery.Builder();

    String[] words = searchText.split(" ");
    for (String word: words) {
        phraseQuery.add(new Term("content", word));
    }
    PhraseQuery pq = phraseQuery.build();


    try {
        TopDocs results = searcher.search(pq, 100);
        ScoreDoc[] scoreDoc = results.scoreDocs;
        for (int i = 0; i < scoreDoc.length; i++ ) {
            Document hitDoc = searcher.doc(scoreDoc[i].doc);
            ids.add(Long.parseLong(hitDoc.get("documentId")));
        }

    } catch (ParseException | IOException e){
        e.toString();
    }
    return ids;
}

谢谢

1 个答案:

答案 0 :(得分:0)

我做到了!!!

..下面的代码搜索类似“一个*”或“一个*两个*”

TopDocs results = null;
try{
    String[] words = searchText.split(" ");
    int count = words.length;
    if (count == 1) {
        PrefixQuery query = new PrefixQuery(new Term("content", searchText));
        results = searcher.search(query, 100);
    } else if (count > 1) {
        SpanQuery[] sq = new SpanQuery[count];
        for (int i = 0; words.length - 1 >= i; i++) {
            sq[i] = new SpanMultiTermQueryWrapper<PrefixQuery>(new PrefixQuery(new 
                Term("content", words[i])));
        }
        SpanNearQuery query = new SpanNearQuery(sq, 0, true);
        results = searcher.search(query, 100);
    }

    ScoreDoc[] scoreDoc = results.scoreDocs;
    for (int i = 0; i < scoreDoc.length; i++ ) {
        Document hitDoc = searcher.doc(scoreDoc[i].doc);
        ids.add(Long.parseLong(hitDoc.get("documentId")));
    }
} catch (IOException e){
    e.toString();
}
相关问题