Project

General

Profile

1
package eu.dnetlib.index.solr.model;
2

    
3
import java.util.Collection;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import org.apache.solr.common.SolrInputDocument;
8
import org.apache.solr.common.SolrInputField;
9

    
10
import eu.dnetlib.clients.index.model.Any.ValueType;
11
import eu.dnetlib.clients.index.model.document.AbstractIndexDocument;
12

    
13
// TODO: Auto-generated Javadoc
14

    
15
/**
16
 * The Class SolrIndexDocument an implementation of the index document for SOLR.
17
 */
18
public class SolrIndexDocument extends AbstractIndexDocument {
19

    
20
	/**
21
	 * Instantiates a new solr index document.
22
	 *
23
	 * @param schema
24
	 *            the schema
25
	 * @param dsId
26
	 *            the ds id
27
	 */
28
	public SolrIndexDocument(final Map<String, ValueType> schema, final String dsId) {
29
		super(schema, dsId);
30
	}
31

    
32
	/**
33
	 * Instantiates a new solr index document.
34
	 *
35
	 * @param schema
36
	 *            the schema
37
	 * @param dsId
38
	 *            the ds id
39
	 * @param solrDocument
40
	 *            the solr document
41
	 */
42
	public SolrIndexDocument(final Map<String, ValueType> schema, final String dsId, final SolrInputDocument solrDocument) {
43
		super(schema, dsId);
44
		addFields(solrDocument);
45
	}
46

    
47
	/**
48
	 * Adds the fields.
49
	 *
50
	 * @param solrDocument
51
	 *            the solr document
52
	 */
53
	private void addFields(final SolrInputDocument solrDocument) {
54
		for (String name : solrDocument.getFieldNames()) {
55
			Collection<Object> fieldValues = solrDocument.getFieldValues(name);
56
			if (fieldValues.size() > 1) {
57
				addField(name, fieldValues);
58
			} else if (fieldValues.size() == 1) {
59
				addField(name, fieldValues.iterator().next());
60
			}
61
		}
62
	}
63

    
64
	/**
65
	 * Sets the content.
66
	 *
67
	 * @param solrDocument
68
	 *            the new content
69
	 */
70
	public void setContent(final SolrInputDocument solrDocument) {
71
		addFields(solrDocument);
72
	}
73

    
74
	/**
75
	 * Gets the solr document.
76
	 *
77
	 * @return the solr document
78
	 */
79
	public SolrInputDocument getSolrDocument() {
80

    
81
		Map<String, SolrInputField> data = new HashMap<>();
82
		for (String key : fields.keySet()) {
83
			SolrInputField solrField = new SolrInputField(key);
84
			for (Object o : fields.get(key)) {
85
				solrField.addValue(o);
86
			}
87
			data.put(key, solrField);
88
		}
89
		return new SolrInputDocument(data);
90
	}
91
}
    (1-1/1)