3
votes

I'm new to Apache Lucene. I'm using the latest version: 6.3.0 in combination with facet library. Based on the examples I found on github: https://github.com/apache/lucene-solr/tree/master/lucene/demo/src/java/org/apache/lucene/demo/facet

I have the following Document

Document doc = new Document();
doc.add(new FacetField("Author", "Bob"));
doc.add(new FacetField("Publish Date", "2010", "10", "15"));
doc.add(new FacetField("Tags", "A"));
doc.add(new FacetField("Tags", "B"));

//[FacetField(dim=Author path=[Bob]), FacetField(dim=Publish Date path=[2010, 10, 15]), FacetField(dim=Tags path=[A]), FacetField(dim=Tags path=[B])]
System.out.println(doc.getFields());

//null
System.out.println(doc.getField("Author"));

doc.getFields() returns all the fields, but doc.getField("Author") returns null. Am I doing something wrong?

Digging further if I do something like this:

for(IndexableField myField:doc.getFields()){
    System.out.println(myField.name());
}

The following is printed:

dummy
dummy
dummy
dummy

And if I do something like this doc.getField("dummy") it will indeed return the first field(Author).

Looking at FacetField source code: https://github.com/apache/lucene-solr/blob/branch_6_3/lucene/facet/src/java/org/apache/lucene/facet/FacetField.java It seems that all facet fields are created with "dummy": https://github.com/apache/lucene-solr/blob/branch_6_3/lucene/facet/src/java/org/apache/lucene/facet/FacetField.java

public FacetField(String dim, String... path) {
super("dummy", TYPE);

Is this a bug?

2

2 Answers

5
votes

I ran into the same issue myself. If you were to write said document and obtain it with a search, you'd see that FacetFields aren't actually stored as part of the document. Additionally, searching on a value in a FacetField doesn't work (returns 0 results). It'd be nice if there was a flag or something that would result in the field being written to both the taxonomy and to the index itself, but I think the issue is that the document index has several other attributes that don't apply to facet fields so they just keep them separate to avoid confusion.

The way I worked around it is by writing the field twice on the document: one as a FacetField and another field with the desired attributes.

Example:

Document doc = new Document();

// Add facet fields, not stored/searchable, but can be drilled down into
doc.add(new FacetField("Author", "Bob"));
...

// Add other fields
doc.add(new TextField("Author", "Bob", Store.YES));
...
-1
votes

As per my understanding, you are trying to retrieve the value of Author, which it should be returned as "Bob".

FacetField & Field are two different type of Fields in Lucene and they store the data in different manner. And FacetField is child class of Field class. To initialize the Field you needs the name of field, type & boolean value which denotes you want to retrieve the field or not.

public class FacetField extends Field

And here is the example of Field initialization

Field pathField = new StringField("path", file.toString(), Field.Store.YES);
doc.add(pathField);

Now, in order to store the documents, you should do it like this.

SolrInputDocument doc = new SolrInputDocument();
String id = "1";
String author = "Erick";
String text = "I love Solr book";
doc.addField("id", id);
doc.addField("author", title);
doc.addField("text", text);

In order to undertand the solr Indeing & Searching using Solrj please rever this link.

I hope this helps.