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 Mike Miller <Mi...@jda.com> on 2005/02/08 14:54:57 UTC

Problem searching Field.Keyword field

First let me say - Awesome tool!  Almost too easy to be true, but with
that being said....
 
Hi,  I have read several articles and postings that indicate that the
Field.Keyword field should be searchable but it's not working for me,
until I change to Field.Text.  Parts of the index and search code are
included below - mostly lifted from articles,etc, including Erik Hatches
article on java.net.   I created a small KnowledgeBase web application
that contains a category field, which I want to be searchable.
Searching using a query string of category:Doc* or
category:Documentation does not find a hit unless I change the code to
add the category to the index as a Field.Text instead of Field.Keyword.
The field value is out there:   I have verified this using the TermEnum
to list the term values for field category and Documentation is in the
list of values.  
 
The intention is to provide a 'Advanced Search' page that allows the
user to search specific fields, like category, title and author instead
of always using the 'all' field.  
 
What am I doing wrong???     Thanks in advance.
 
Index code:
 
public boolean index(ArticleFormBean article) throws IOException {
  IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
false);
 
         Document doc = new Document();
         doc.add(Field.UnStored("content", article.getContent()));
        doc.add(Field.Text("title", article.getTitle()));
        doc.add(Field.Text("author", article.getAuthor()));
        doc.add(Field.UnIndexed("articleId",
String.valueOf(article.getArticleId())));
        doc.add(Field.Keyword("createdDate", article.getCreateDate()));
        doc.add(Field.Keyword("modDate", article.getModDate()));
        doc.add(Field.Keyword("category", article.getCategory()));

        // create an 'all' field
        StringBuffer sb = new StringBuffer(4000);
        sb.append(article.getTitle()).append("
").append(article.getAuthor()).append(" ");
        sb.append(article.getContent()).append("
").append(article.getCategory());
        doc.add(Field.UnStored("all", sb.toString()));
        
        writer.addDocument(doc);
        writer.optimize();
        writer.close();
        
        return false;
 }
 
Search code:
            File indexDir = new File("c:/dev/java/kb/index");
            Directory fsDir = FSDirectory.getDirectory(indexDir, false);
            IndexSearcher is = new IndexSearcher(fsDir);
            Query query = QueryParser.parse(q, "all", new
StandardAnalyzer());
            Hits hits = is.search(query);
            

 
Mike Miller
JDA Software Group, Inc.
7501 Ester's Blvd, Suite 100
Irving, Texas 75063
 

Re: Problem searching Field.Keyword field

Posted by Miles Barr <mi...@runtime-collective.com>.
You're using the query parser with the standard analyser. You should
construct a term query manually instead.

 
-- 
Miles Barr <mi...@runtime-collective.com>
Runtime Collective Ltd.

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


Re: Problem searching Field.Keyword field

Posted by Kelvin Tan <ke...@relevanz.com>.
Javadocs for Field.Keyword says:

Constructs a Date-valued Field that is not tokenized and is indexed, and stored in the index, for return with hits.

For most purposes dealing with Strings, use Field.Text, unless you have a date, a GUID or some other string you don't want tokenized or processed in any way. This basically means that Field.Keyword indexes the field as-is.

k

On Tue, 8 Feb 2005 07:54:57 -0600, Mike Miller wrote:
>�First let me say - Awesome tool! �Almost too easy to be true, but
>�with that being said....
>
>�Hi, �I have read several articles and postings that indicate that
>�the Field.Keyword field should be searchable but it's not working
>�for me, until I change to Field.Text. �Parts of the index and
>�search code are included below - mostly lifted from articles,etc,
>�including Erik Hatches article on java.net. � I created a small
>�KnowledgeBase web application that contains a category field, which
>�I want to be searchable. Searching using a query string of
>�category:Doc* or
>�category:Documentation does not find a hit unless I change the code
>�to add the category to the index as a Field.Text instead of
>�Field.Keyword. The field value is out there: � I have verified this
>�using the TermEnum to list the term values for field category and
>�Documentation is in the list of values.
>
>�The intention is to provide a 'Advanced Search' page that allows
>�the user to search specific fields, like category, title and author
>�instead of always using the 'all' field.
>
>�What am I doing wrong??? � � Thanks in advance.
>
>�Index code:
>
>�public boolean index(ArticleFormBean article) throws IOException {
>�IndexWriter writer = new IndexWriter(indexDir, new
>�StandardAnalyzer(), false);
>
>�Document doc = new Document();
>�doc.add(Field.UnStored("content", article.getContent()));
>�doc.add(Field.Text("title", article.getTitle()));
>�doc.add(Field.Text("author", article.getAuthor()));
>�doc.add(Field.UnIndexed("articleId",
>�String.valueOf(article.getArticleId())));
>�doc.add(Field.Keyword("createdDate", article.getCreateDate()));
>�doc.add(Field.Keyword("modDate", article.getModDate()));
>�doc.add(Field.Keyword("category", article.getCategory()));
>
>�// create an 'all' field
>�StringBuffer sb = new StringBuffer(4000);
>�sb.append(article.getTitle()).append("
>�").append(article.getAuthor()).append(" ");
>�sb.append(article.getContent()).append("
>�").append(article.getCategory());
>�doc.add(Field.UnStored("all", sb.toString()));
>
>�writer.addDocument(doc);
>�writer.optimize();
>�writer.close();
>
>�return false;
>�}
>
>�Search code:
>�File indexDir = new File("c:/dev/java/kb/index"); Directory fsDir =
>�FSDirectory.getDirectory(indexDir, false); IndexSearcher is = new
>�IndexSearcher(fsDir); Query query = QueryParser.parse(q, "all", new
>�StandardAnalyzer()); Hits hits = is.search(query);
>
>
>�Mike Miller
>�JDA Software Group, Inc.
>�7501 Ester's Blvd, Suite 100
>�Irving, Texas 75063



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