Lucene: Multi-word phrases as search terms

后端 未结 4 1412
我寻月下人不归
我寻月下人不归 2020-12-03 15:46

I\'m trying to make a searchable phone/local business directory using Apache Lucene.

I have fields for street name, business name, phone number etc. The problem tha

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 16:07

    I found that my attempt to generate a query without using a QueryParser was not working, so I stopped trying to create my own queries and used a QueryParser instead. All of the recomendations that I saw online showed that you should use the same Analyzer in the QueryParser that you use during indexing, so I used a StandardAnalyzer to build the QueryParser.

    This works on this example because the StandardAnalyzer removes the word "the" from the street "the crescent" during indexing, and hence we can't search for it because it isn't in the index.

    However, if we choose to search for "Grove Road", we have a problem with the out-of-the-box functionality, namely that the query will return all of the results containing either "Grove" OR "Road". This is easily fixed by setting up the QueryParser so that it's default operation is AND instead of OR.

    In the end, the correct solution was the following:

    int numberOfHits = 200;
    String LocationOfDirectory = "C:\\dir\\index";
    TopScoreDocCollector collector = TopScoreDocCollector.create(numberOfHits, true);
    Directory directory = new SimpleFSDirectory(new File(LocationOfDirectory));
    IndexSearcher searcher = new IndexSearcher(IndexReader.open(directory);
    
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
    
    //WildcardQuery q = new WildcardQuery(new Term("Street", "the crescent");
    QueryParser qp = new QueryParser(Version.LUCENE_35, "Street", analyzer);
    qp.setDefaultOperator(QueryParser.Operator.AND);
    
    Query q = qp.parse("grove road");
    
    searcher.search(q, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    

提交回复
热议问题