You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by illusionz <de...@gmail.com> on 2013/09/21 08:48:00 UTC

lucene 4.3 search not working ShingleAnalyzerWrapper

Hi,

I am learning lucene, and have created indexes using LuceneWriter (which
worked fine), but when I try an query it with LuceneReader it dose not
work, need help on the same.  Following is the code for LuceneWriter.java
and LuceneReader.java:


public class LuceneWriter {
 public static void main(String[] arg) throws IOException
{
File docDir = new File(DataConfig.dataDir);
 Directory dir =  FSDirectory.open(docDir);
HashMap<String, Analyzer> documentFiledAnalysers = new HashMap<String,
Analyzer>();
 documentFiledAnalysers.put("id", new StandardAnalyzer(Version.LUCENE_43));
 documentFiledAnalysers.put("content", new ShingleAnalyzerWrapper(new
StandardAnalyzer(Version.LUCENE_43), 2, 2, "", false, true));
 PerFieldAnalyzerWrapper analyserWrapper = new PerFieldAnalyzerWrapper(new
StandardAnalyzer(Version.LUCENE_43), documentFiledAnalysers);
 IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
analyserWrapper);
 iwc.setOpenMode(OpenMode.CREATE);
 IndexWriter writer =  new IndexWriter(dir, iwc);
 Document doc2 = new Document();
doc2.add(new StringField("id", "2", Field.Store.YES));
doc2.add(new StringField("content", "I am leagend",
Field.Store.<http://field.store.no/>
YES));
 Document doc3 = new Document();
doc3.add(new StringField("id", "3", Field.Store.YES));
doc3.add(new StringField("content", "Tom got a cat.",
Field.Store.<http://field.store.no/>
YES));
 writer.addDocument(doc2);
writer.addDocument(doc3);

 writer.close();
 System.out.println("------------ Added index -----------");
 }

}



public class LuceneReader {


/**

* @param args

*/

public static void main(String[] args) {

try {

IndexReader reader =
DirectoryReader.open(FSDirectory.open(new File(DataConfig.dataDir)));

IndexSearcher searcher =  new IndexSearcher(reader);

HashMap<String, Analyzer> documentFieldAnalysers = new HashMap<String,
Analyzer>();

documentFieldAnalysers.put("id", new StandardAnalyzer(Version.LUCENE_43));

documentFieldAnalysers.put("content", new ShingleAnalyzerWrapper(new
StandardAnalyzer(Version.LUCENE_43),
2, 2, "", false, true));

PerFieldAnalyzerWrapper analyserWrapper
= new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_43),
documentFieldAnalysers);

QueryParser qpHelper = new QueryParser(Version.LUCENE_43, "content",
analyserWrapper);

Query query = qpHelper.parse("amleagend");

TopDocs results = searcher.search(query, 1000);

System.out.println("Searching for: " + query);

ScoreDoc[] hits = results.scoreDocs;



for(int i =0; i < hits.length; i++)

{

  Document doc = searcher.doc(hits[i].doc);

  System.out.println(doc);

}





} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}




}


Thanks,

Depanker Sharma

Re: lucene 4.3 search not working ShingleAnalyzerWrapper

Posted by Michael McCandless <lu...@mikemccandless.com>.
What Query is actually parsed?

Mike McCandless

http://blog.mikemccandless.com


On Sat, Sep 21, 2013 at 8:48 PM, illusionz <de...@gmail.com> wrote:
> Hi Michael,
>
> I am getting no hits.
>
> Tokens are generated in this form : iam, amleagend
>
>  I am searching from "amleagend", and getting no hits.
>
> Thanks,
> Depanker Sharma
>
>
>
>
> On Sat, Sep 21, 2013 at 10:38 PM, Michael McCandless <
> lucene@mikemccandless.com> wrote:
>
>> What's actually going wrong?  Exception?  No hits?
>>
>> What query is actually being executed?  Ie what does that
>> System.out.println print?
>>
>> If you search for i am leagend instead does it still find no hits?
>>
>> Mike McCandless
>>
>> http://blog.mikemccandless.com
>>
>>
>> On Sat, Sep 21, 2013 at 2:48 AM, illusionz <de...@gmail.com>
>> wrote:
>> > Hi,
>> >
>> > I am learning lucene, and have created indexes using LuceneWriter (which
>> > worked fine), but when I try an query it with LuceneReader it dose not
>> > work, need help on the same.  Following is the code for LuceneWriter.java
>> > and LuceneReader.java:
>> >
>> >
>> > public class LuceneWriter {
>> >  public static void main(String[] arg) throws IOException
>> > {
>> > File docDir = new File(DataConfig.dataDir);
>> >  Directory dir =  FSDirectory.open(docDir);
>> > HashMap<String, Analyzer> documentFiledAnalysers = new HashMap<String,
>> > Analyzer>();
>> >  documentFiledAnalysers.put("id", new
>> StandardAnalyzer(Version.LUCENE_43));
>> >  documentFiledAnalysers.put("content", new ShingleAnalyzerWrapper(new
>> > StandardAnalyzer(Version.LUCENE_43), 2, 2, "", false, true));
>> >  PerFieldAnalyzerWrapper analyserWrapper = new
>> PerFieldAnalyzerWrapper(new
>> > StandardAnalyzer(Version.LUCENE_43), documentFiledAnalysers);
>> >  IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
>> > analyserWrapper);
>> >  iwc.setOpenMode(OpenMode.CREATE);
>> >  IndexWriter writer =  new IndexWriter(dir, iwc);
>> >  Document doc2 = new Document();
>> > doc2.add(new StringField("id", "2", Field.Store.YES));
>> > doc2.add(new StringField("content", "I am leagend",
>> > Field.Store.<http://field.store.no/>
>> > YES));
>> >  Document doc3 = new Document();
>> > doc3.add(new StringField("id", "3", Field.Store.YES));
>> > doc3.add(new StringField("content", "Tom got a cat.",
>> > Field.Store.<http://field.store.no/>
>> > YES));
>> >  writer.addDocument(doc2);
>> > writer.addDocument(doc3);
>> >
>> >  writer.close();
>> >  System.out.println("------------ Added index -----------");
>> >  }
>> >
>> > }
>> >
>> >
>> >
>> > public class LuceneReader {
>> >
>> >
>> > /**
>> >
>> > * @param args
>> >
>> > */
>> >
>> > public static void main(String[] args) {
>> >
>> > try {
>> >
>> > IndexReader reader =
>> > DirectoryReader.open(FSDirectory.open(new File(DataConfig.dataDir)));
>> >
>> > IndexSearcher searcher =  new IndexSearcher(reader);
>> >
>> > HashMap<String, Analyzer> documentFieldAnalysers = new HashMap<String,
>> > Analyzer>();
>> >
>> > documentFieldAnalysers.put("id", new
>> StandardAnalyzer(Version.LUCENE_43));
>> >
>> > documentFieldAnalysers.put("content", new ShingleAnalyzerWrapper(new
>> > StandardAnalyzer(Version.LUCENE_43),
>> > 2, 2, "", false, true));
>> >
>> > PerFieldAnalyzerWrapper analyserWrapper
>> > = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_43),
>> > documentFieldAnalysers);
>> >
>> > QueryParser qpHelper = new QueryParser(Version.LUCENE_43, "content",
>> > analyserWrapper);
>> >
>> > Query query = qpHelper.parse("amleagend");
>> >
>> > TopDocs results = searcher.search(query, 1000);
>> >
>> > System.out.println("Searching for: " + query);
>> >
>> > ScoreDoc[] hits = results.scoreDocs;
>> >
>> >
>> >
>> > for(int i =0; i < hits.length; i++)
>> >
>> > {
>> >
>> >   Document doc = searcher.doc(hits[i].doc);
>> >
>> >   System.out.println(doc);
>> >
>> > }
>> >
>> >
>> >
>> >
>> >
>> > } catch (Exception e) {
>> >
>> > // TODO Auto-generated catch block
>> >
>> > e.printStackTrace();
>> >
>> > }
>> >
>> >
>> > }
>> >
>> >
>> >
>> >
>> > }
>> >
>> >
>> > Thanks,
>> >
>> > Depanker Sharma
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: lucene 4.3 search not working ShingleAnalyzerWrapper

Posted by illusionz <de...@gmail.com>.
Hi Michael,

I am getting no hits.

Tokens are generated in this form : iam, amleagend

 I am searching from "amleagend", and getting no hits.

Thanks,
Depanker Sharma




On Sat, Sep 21, 2013 at 10:38 PM, Michael McCandless <
lucene@mikemccandless.com> wrote:

> What's actually going wrong?  Exception?  No hits?
>
> What query is actually being executed?  Ie what does that
> System.out.println print?
>
> If you search for i am leagend instead does it still find no hits?
>
> Mike McCandless
>
> http://blog.mikemccandless.com
>
>
> On Sat, Sep 21, 2013 at 2:48 AM, illusionz <de...@gmail.com>
> wrote:
> > Hi,
> >
> > I am learning lucene, and have created indexes using LuceneWriter (which
> > worked fine), but when I try an query it with LuceneReader it dose not
> > work, need help on the same.  Following is the code for LuceneWriter.java
> > and LuceneReader.java:
> >
> >
> > public class LuceneWriter {
> >  public static void main(String[] arg) throws IOException
> > {
> > File docDir = new File(DataConfig.dataDir);
> >  Directory dir =  FSDirectory.open(docDir);
> > HashMap<String, Analyzer> documentFiledAnalysers = new HashMap<String,
> > Analyzer>();
> >  documentFiledAnalysers.put("id", new
> StandardAnalyzer(Version.LUCENE_43));
> >  documentFiledAnalysers.put("content", new ShingleAnalyzerWrapper(new
> > StandardAnalyzer(Version.LUCENE_43), 2, 2, "", false, true));
> >  PerFieldAnalyzerWrapper analyserWrapper = new
> PerFieldAnalyzerWrapper(new
> > StandardAnalyzer(Version.LUCENE_43), documentFiledAnalysers);
> >  IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
> > analyserWrapper);
> >  iwc.setOpenMode(OpenMode.CREATE);
> >  IndexWriter writer =  new IndexWriter(dir, iwc);
> >  Document doc2 = new Document();
> > doc2.add(new StringField("id", "2", Field.Store.YES));
> > doc2.add(new StringField("content", "I am leagend",
> > Field.Store.<http://field.store.no/>
> > YES));
> >  Document doc3 = new Document();
> > doc3.add(new StringField("id", "3", Field.Store.YES));
> > doc3.add(new StringField("content", "Tom got a cat.",
> > Field.Store.<http://field.store.no/>
> > YES));
> >  writer.addDocument(doc2);
> > writer.addDocument(doc3);
> >
> >  writer.close();
> >  System.out.println("------------ Added index -----------");
> >  }
> >
> > }
> >
> >
> >
> > public class LuceneReader {
> >
> >
> > /**
> >
> > * @param args
> >
> > */
> >
> > public static void main(String[] args) {
> >
> > try {
> >
> > IndexReader reader =
> > DirectoryReader.open(FSDirectory.open(new File(DataConfig.dataDir)));
> >
> > IndexSearcher searcher =  new IndexSearcher(reader);
> >
> > HashMap<String, Analyzer> documentFieldAnalysers = new HashMap<String,
> > Analyzer>();
> >
> > documentFieldAnalysers.put("id", new
> StandardAnalyzer(Version.LUCENE_43));
> >
> > documentFieldAnalysers.put("content", new ShingleAnalyzerWrapper(new
> > StandardAnalyzer(Version.LUCENE_43),
> > 2, 2, "", false, true));
> >
> > PerFieldAnalyzerWrapper analyserWrapper
> > = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_43),
> > documentFieldAnalysers);
> >
> > QueryParser qpHelper = new QueryParser(Version.LUCENE_43, "content",
> > analyserWrapper);
> >
> > Query query = qpHelper.parse("amleagend");
> >
> > TopDocs results = searcher.search(query, 1000);
> >
> > System.out.println("Searching for: " + query);
> >
> > ScoreDoc[] hits = results.scoreDocs;
> >
> >
> >
> > for(int i =0; i < hits.length; i++)
> >
> > {
> >
> >   Document doc = searcher.doc(hits[i].doc);
> >
> >   System.out.println(doc);
> >
> > }
> >
> >
> >
> >
> >
> > } catch (Exception e) {
> >
> > // TODO Auto-generated catch block
> >
> > e.printStackTrace();
> >
> > }
> >
> >
> > }
> >
> >
> >
> >
> > }
> >
> >
> > Thanks,
> >
> > Depanker Sharma
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

Re: lucene 4.3 search not working ShingleAnalyzerWrapper

Posted by Michael McCandless <lu...@mikemccandless.com>.
What's actually going wrong?  Exception?  No hits?

What query is actually being executed?  Ie what does that
System.out.println print?

If you search for i am leagend instead does it still find no hits?

Mike McCandless

http://blog.mikemccandless.com


On Sat, Sep 21, 2013 at 2:48 AM, illusionz <de...@gmail.com> wrote:
> Hi,
>
> I am learning lucene, and have created indexes using LuceneWriter (which
> worked fine), but when I try an query it with LuceneReader it dose not
> work, need help on the same.  Following is the code for LuceneWriter.java
> and LuceneReader.java:
>
>
> public class LuceneWriter {
>  public static void main(String[] arg) throws IOException
> {
> File docDir = new File(DataConfig.dataDir);
>  Directory dir =  FSDirectory.open(docDir);
> HashMap<String, Analyzer> documentFiledAnalysers = new HashMap<String,
> Analyzer>();
>  documentFiledAnalysers.put("id", new StandardAnalyzer(Version.LUCENE_43));
>  documentFiledAnalysers.put("content", new ShingleAnalyzerWrapper(new
> StandardAnalyzer(Version.LUCENE_43), 2, 2, "", false, true));
>  PerFieldAnalyzerWrapper analyserWrapper = new PerFieldAnalyzerWrapper(new
> StandardAnalyzer(Version.LUCENE_43), documentFiledAnalysers);
>  IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
> analyserWrapper);
>  iwc.setOpenMode(OpenMode.CREATE);
>  IndexWriter writer =  new IndexWriter(dir, iwc);
>  Document doc2 = new Document();
> doc2.add(new StringField("id", "2", Field.Store.YES));
> doc2.add(new StringField("content", "I am leagend",
> Field.Store.<http://field.store.no/>
> YES));
>  Document doc3 = new Document();
> doc3.add(new StringField("id", "3", Field.Store.YES));
> doc3.add(new StringField("content", "Tom got a cat.",
> Field.Store.<http://field.store.no/>
> YES));
>  writer.addDocument(doc2);
> writer.addDocument(doc3);
>
>  writer.close();
>  System.out.println("------------ Added index -----------");
>  }
>
> }
>
>
>
> public class LuceneReader {
>
>
> /**
>
> * @param args
>
> */
>
> public static void main(String[] args) {
>
> try {
>
> IndexReader reader =
> DirectoryReader.open(FSDirectory.open(new File(DataConfig.dataDir)));
>
> IndexSearcher searcher =  new IndexSearcher(reader);
>
> HashMap<String, Analyzer> documentFieldAnalysers = new HashMap<String,
> Analyzer>();
>
> documentFieldAnalysers.put("id", new StandardAnalyzer(Version.LUCENE_43));
>
> documentFieldAnalysers.put("content", new ShingleAnalyzerWrapper(new
> StandardAnalyzer(Version.LUCENE_43),
> 2, 2, "", false, true));
>
> PerFieldAnalyzerWrapper analyserWrapper
> = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_43),
> documentFieldAnalysers);
>
> QueryParser qpHelper = new QueryParser(Version.LUCENE_43, "content",
> analyserWrapper);
>
> Query query = qpHelper.parse("amleagend");
>
> TopDocs results = searcher.search(query, 1000);
>
> System.out.println("Searching for: " + query);
>
> ScoreDoc[] hits = results.scoreDocs;
>
>
>
> for(int i =0; i < hits.length; i++)
>
> {
>
>   Document doc = searcher.doc(hits[i].doc);
>
>   System.out.println(doc);
>
> }
>
>
>
>
>
> } catch (Exception e) {
>
> // TODO Auto-generated catch block
>
> e.printStackTrace();
>
> }
>
>
> }
>
>
>
>
> }
>
>
> Thanks,
>
> Depanker Sharma

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org