24

Will someone please explain under what circumstance I may use Field.Store.NO instead of Field.Store.YES? I am extremely new to Lucene. And I am trying to create a document. Per my basic knowledge, I am doing

doc.add(new StringField(fieldNameA,fieldValueA,Field.Store.YES));
doc.add(new TextField(fieldNameB,fieldValueB,Field.Store.YES));
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

2 Answers2

44

There are two basic ways a document can be written into Lucene.

  • Indexed - The field is analyzed and indexed, and can be searched.
  • Stored - The field's full text is stored and will be returned with search results.

If a document is indexed but not stored, you can search for it, but it won't be returned with search results.

One reasonably common pattern is to use lucene for search, but only have an ID field being stored which can be used to retrieve the full contents of the document/record from, for instance, a SQL database, a file system, or an web resource.

You might also opt not to store a field when that field is just a search tool, but you wouldn't display it to the user, such as a soundex/metaphone, or an alternate analysis of a content field.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • 2
    Thanks for the reply. For the sake of exactness/completeness: if all I want back is the documentId, then I should use `Field.Store.NO`: the search will be conducted with no perceivable difference, but only the documentId will be return? Also do I have to explicitly specify `fieldType.setIndexed(true)`? And the document id is simply `scoreDoc.doc`? – Katedral Pillon Oct 05 '15 at 17:24
  • 3
    Right, with the pattern, your documentId could be the only field set to `Store.YES`. Whether fields are stored or not has *no* impact on which documents are matched when searching, only what gets returned when you retrieve a doc (ex. from [`IndexSearcher.doc`](https://lucene.apache.org/core/5_3_0/core/index.html?org/apache/lucene/search/IndexSearcher.html)). On what to use for an identifier, if you are attempting to identify the document from an external resource, I wouldn't use Lucene's internal DocID. Use a key from the database, or a file system path, or whatever suits the situation. – femtoRgon Oct 05 '15 at 17:54
  • You don't usually need to specify whether you want the document indexed or not. `TextField`, `StringField`, etc. each have their own implicit strategy on how the document should be analyzed (`TextField` is indexed and analyzed, `StringField` is indexed but not analyzed, `StoredField` is not indexed). – femtoRgon Oct 05 '15 at 17:56
5

Use Field.Store.YES when you need a document back from Lucene document. Use NO when you just need a search from document. Here is a link explained with a scenario. https://handyopinion.com/java-lucene-saving-fields-or-not/

Ahmed Bilal
  • 137
  • 2
  • 11