Implementation Analysis of SOLR multivalue

Source: Internet
Author: User

Online businesses are ready to use SOLR for data storage and indexing. Some of the fields must be stored in multiple words.
Multivalue can implement this function.

<dynamicField name="*_ss" type="string"  indexed="true"  stored="true" multiValued="true"/>

Let's take a look at its implementation principles:
Two classes related to writing documents to SOLR are solrinputdocument and solrinputfield. solrinputdocument is related to the entire document, and solrinputfield is related to field (attributes include field names, values, and boost values ).
In the solrinputdocument class, addfield and setfield are used to add methods to the document. setfield overwrites the previous value and addfield appends the value.
Let's take a look at its specific implementation:

Public solrinputdocument () {_ fields = new linkedhashmap <string, solrinputfield> (); // construct a map using the constructor. The value is solrinputfield} private final map <string, solrinputfield> _ fields; in the public void addfield (string name, object value, float boost) // addfiled method, the parameter value is an object solrinputfield field = _ fields. get (name); // name indicates the name of the field, and value indicates the value of the field, determine whether the map has the information of this field. If (field = NULL | field. value = NULL) {// If the filed or filed value is null, use the setfield of this class (that is, add a valid value for the first time, similar to overwrite) setfield (name, value, boost);} else {field. addvalue (value, boost); // otherwise, use the addvalue method of solrinputfield (similar to append )}}

The setfield implementation is as follows:

Public void setfield (string name, object value, float boost) {solrinputfield field = new solrinputfield (name); _ fields. put (name, field); field. setvalue (value, boost); // actually called the setvalue method of solrinputfield}

Let's take a look at the solrinputfield class:

Public class solrinputfield implements iterable <Object>, serializable {string name; object value = NULL; float boost = 1.0f; Public solrinputfield (string N) {This. name = N;} public void setvalue (Object V, float B) {boost = B; If (V instanceof object []) {// arrays will be converted to a collection. if the input value is a list, it is converted to collection object [] arr = (object []) V; collection <Object> C = new arraylist <Object> (ARR. length); For (Object O: ARR) {C. add (o) ;}value = C ;}else {value = V ;}} public void addvalue (Object V, float B) {// you can see whether it is a collection if (value = NULL) {If (V instanceof collection) {collection <Object> C = new arraylist <Object> (3); For (Object O: (collection <Object>) V) {C. add (o) ;}setvalue (C, B) ;}else {setvalue (V, B) ;}return ;}

....

We can see from the above that the data structure such as array can be passed in to the field, so that multiple values can be inserted in a field.
For example, the following two methods can write multiple valid values in the same field:
Example 1:

SolrInputDocument doc = new SolrInputDocument(); String key = "123"; doc.addField("id", key); doc.addField("test_ss", "vv1"); doc.addField("test_ss", "vv2"); doc.addField("test_ss", "vv3");

Example 2:

SolrInputDocument doc = new SolrInputDocument();String key = "123";String[] vv = {"vv1","vv2","vv3"};doc.addField("id", key);doc.addField("test_ss", vv);

Note: every time you instantiate a solrinputdocument object, it is equivalent to re-overwriting this line, because SOLR is based on Lucene, and Lucene update is implemented through Delete + Add. That is, if a document is ID: 1, test_ss: AA, after re-instantiation, addfiled ("test_ss", "BB") is used for the same ID record ") after that, the document becomes ID: 1, test_ss: BB .. (Both setfield overwrite and addfield append are for this instance ).

This article from the "Food light blog" blog, please be sure to keep this source http://caiguangguang.blog.51cto.com/1652935/1433761

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.