Note that there are some explanatory texts on larger screens.

plurals
  1. POPhraseQuery Not working Lucene 4.5.0
    primarykey
    data
    text
    <p>I tried to work with <code>PhraseQuery</code> but could not get hits from search. I am using <code>Lucene 4.5.0</code>. </p> <p><strong>My Indexing code</strong></p> <pre><code>private IndexWriter writer; public LuceneIndexSF(final String indexDir) throws IOException { Analyzer analyzer = new KeywordAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_45, analyzer); Directory directory = FSDirectory.open(new File(indexDir)); writer = new IndexWriter(directory, config); } private Document getDocument(File f, String line, int lineNum) throws IOException { Document doc = new Document(); Field field = null; if (line != null &amp;&amp; line.split(DELIMITER).length &gt;= 5) { String[] lineValues = line.split(DELIMITER); field = new Field("name", line.split("\t")[1], TextField.TYPE_STORED); doc.add(field); if (lineValues[2] != null &amp;&amp; !lineValues[2].trim().isEmpty()) { field = new Field("ref", lineValues[2], TextField.TYPE_STORED); doc.add(field); } field = new Field("type", lineValues[3], TextField.TYPE_STORED); doc.add(field); field = new LongField("code", Long.parseLong(lineValues[4]), LongField.TYPE_STORED); doc.add(field); if (lineValues.length == 7 &amp;&amp; lineValues[5] != null &amp;&amp; !lineValues[5].trim().isEmpty()) { field = new Field("alias1", lineValues[5], TextField.TYPE_STORED); doc.add(field); } if (lineValues.length == 7 &amp;&amp; lineValues[6] != null &amp;&amp; !lineValues[6].trim().isEmpty()) { field = new Field("alias2", lineValues[6], TextField.TYPE_STORED); doc.add(field); } } field = new IntField("linenum", lineNum, IntField.TYPE_STORED); doc.add(field); return doc; } .... and other code where i add document in writer using writer.addDocument(doc); </code></pre> <p><strong>My Searching Code</strong></p> <pre><code>private static void search(String indexDir, String quer) throws IOException, ParseException { IndexReader inxRead = DirectoryReader.open(FSDirectory.open(new File( indexDir))); IndexSearcher is = new IndexSearcher(inxRead); String[] termArr = quer.split(" "); PhraseQuery phraseQuery= new PhraseQuery(); for(int inx = 0; inx &lt; termArr.length; inx++){ phraseQuery.add(new Term("name", termArr[inx])); } phraseQuery.setSlop(4); long start = System.currentTimeMillis(); TopDocs hits = is.search(phraseQuery, 1000); long end = System.currentTimeMillis(); System.err.println("Parser&gt; Found " + hits.totalHits + " document(s) (in " + (end - start) + " milliseconds) that matched query '" + multiQuery + "':"); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println("Parser&gt; " + scoreDoc.score + " :: " + doc.get("type") + " - " + doc.get("code") + " - " + doc.get("name") + ", " + doc.get("linenum")); } inxRead.close(); } </code></pre> <p>Please tell me if i am doing any thing wrong.</p> <p><strong>Edit</strong></p> <p>also tried with Standard Analyzer still not results</p> <pre><code>Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45); </code></pre> <p><strong>Solution</strong></p> <p>According to Arun's answer <code>PhraseQuery</code> to work properly requires Analyzer which Tokenize each word in <code>Document</code>'s <code>Field</code> for my case i used <code>LowerCaseFilter</code> with making all queries lower case so that it can work without case sensitivity. And used <code>EdgeNGramTokenFilter</code> which for auto completion purposes.</p> <pre><code>public LuceneIndexSF(final String indexDir) throws IOException { Analyzer analyzer = new Analyzer() { @Override protected TokenStreamComponents createComponents(String fieldName, java.io.Reader reader) { Tokenizer source = new StandardTokenizer(Version.LUCENE_45, reader); TokenStream result = new StandardFilter(Version.LUCENE_45, source); result = new LowerCaseFilter(Version.LUCENE_45, result); result = new EdgeNGramTokenFilter(Version.LUCENE_45, result, 1, 20); return new TokenStreamComponents(source, result); } }; IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_45, analyzer); Directory directory = FSDirectory.open(new File(indexDir)); writer = new IndexWriter(directory, config); } </code></pre> <p><strong>My final search method</strong></p> <pre><code>private static void search(String indexDir, String quer) throws IOException, ParseException { IndexReader inxRead = DirectoryReader.open(FSDirectory.open(new File( indexDir))); IndexSearcher is = new IndexSearcher(inxRead); String[] termArr = quer.split(" "); PhraseQuery query1 = new PhraseQuery(); PhraseQuery query2 = new PhraseQuery(); PhraseQuery query3 = new PhraseQuery(); for (int inx = 0; inx &lt; termArr.length; inx++) { query1.add(new Term(SchoolFinderConstant.ENTITY_NAME,termArr[inx]),inx); query2.add(new Term(SchoolFinderConstant.ENTITY_ALIAS1,termArr[inx]),inx); query3.add(new Term(SchoolFinderConstant.ENTITY_ALIAS2,termArr[inx]),inx); } BooleanQuery mainQuery = new BooleanQuery(); mainQuery.add(query1, Occur.SHOULD); mainQuery.add(query2, Occur.SHOULD); mainQuery.add(query3, Occur.SHOULD); long start = System.currentTimeMillis(); TopDocs hits = is.search(mainQuery, 1000); long end = System.currentTimeMillis(); System.err.println("Parser&gt; Found " + hits.totalHits + " document(s) (in " + (end - start) + " milliseconds) that matched query '" + multiQuery + "':"); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println("Parser&gt; " + scoreDoc.score + " :: " + doc.get("type") + " - " + doc.get("code") + " - " + doc.get("name") + ", " + doc.get("linenum")); } inxRead.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload