Project

General

Profile

1
package eu.dnetlib.functionality.index.model.util;
2

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

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

    
9
import eu.dnetlib.functionality.index.model.Any.ValueType;
10
import eu.dnetlib.functionality.index.model.document.AbstractIndexDocument;
11

    
12
// TODO: Auto-generated Javadoc
13
/**
14
 * The Class SolrIndexDocument an implementation of the index document for SOLR.
15
 */
16
public class SolrIndexDocument extends AbstractIndexDocument {
17

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

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

    
45
	/**
46
	 * Adds the fields.
47
	 *
48
	 * @param solrDocument
49
	 *            the solr document
50
	 */
51
	private void addFields(final SolrInputDocument solrDocument) {
52
		for (String name : solrDocument.getFieldNames()) {
53
			Object fieldValue = solrDocument.getFieldValue(name);
54
			addField(name, fieldValue);
55
		}
56
	}
57

    
58
	/**
59
	 * Sets the content.
60
	 *
61
	 * @param solrDocument
62
	 *            the new content
63
	 */
64
	public void setContent(final SolrInputDocument solrDocument) {
65
		addFields(solrDocument);
66
	}
67

    
68
	/**
69
	 * Gets the solr document.
70
	 *
71
	 * @return the solr document
72
	 */
73
	public SolrInputDocument getSolrDocument() {
74

    
75
		Map<String, SolrInputField> data = new HashMap<String, SolrInputField>();
76
		for (String key : fields.keySet()) {
77
			SolrInputField solrField = new SolrInputField(key);
78
			for (Object o : fields.get(key)) {
79
				solrField.addValue(o, 1.0f);
80
			}
81
			data.put(key, solrField);
82
		}
83
		return new SolrInputDocument(data);
84
	}
85
}
(2-2/2)