Project

General

Profile

1
package eu.dnetlib.functionality.index.query;
2

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

    
7
import eu.dnetlib.functionality.index.model.Any.ValueType;
8
import eu.dnetlib.functionality.index.model.document.AbstractIndexDocument;
9
import org.apache.solr.common.SolrInputDocument;
10
import org.apache.solr.common.SolrInputField;
11

    
12
// TODO: Auto-generated Javadoc
13

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

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

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

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

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

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

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