Lucene complex data type storage
Lucene uses Field as key-value storage unit, and Field value can be stored for String, int, long, double, float and byte []. Complex data types, such as List, Map and so on, often need to be stored in the development process. The following will show you how to convert complex objects into a single key-value and store them in lucene.
Lucene supports multi-domain value storage, and the same Document can store multiple Field with the same key. Simply understand that lucene supports key=value and key= [value1,value2] storage. All we need to do is convert the object to key=value or key= [value1,value2,...] Is stored in the format of
For example, a row of data in the user table user is
{
"user_id": "00000001"
"user_name": "test1"
"age": 30
"sex": 1
"emails": ["test1_1@126.com", "test1_2@126.com"]
"families": {
"children": [
{
"name": "son1"
"age": 5
"sex": 1
"birth": "2013-08-08"
}
{
"name": "son2"
"age": 1
"sex": 1
"birth": "2017-01-01"
}
]
"partner": {
"name": "wife"
"age": 28
"sex": 2
"birth": "1990-01-01"
}
}
"state": "A"
"create_time": 15648784644
"update_time": 15648784644
}
These data, in addition to families, other fields can be stored directly. Families itself can be converted directly to json string storage, but this makes it impossible to use the data filtering conditions in families. For example, query users whose children age age is greater than or equal to 5. Families can be split and converted to families.children and families.partner storage. The split key-value is:
User_id= "00000001"
User_name= "test1"
Age=30
Sex=1
Emails= ["test1_1@126.com", "test1_2@126.com"]
Families.children.name= ["son1", "son2"]
Families.children.age= [5,1]
Families.children.sex= [1]
Families.children.birth= ["2013-08-08", "2017-01-01"]
Families.partner.name= "wife"
Families.partner.age=28
Families.partner.sex=2
Families.partner.birth= "1990-01-01"
State= "A"
Create_time=15648784644
Update_time=15648784644
In this way, a complex object is converted into multiple key-value stores. To query users with children age age greater than or equal to 5, you only need to set the condition NumericRangeQuery.newIntRange ("families.children.age", 5, Integer.MAX_VALUE, true, true).
The above explains how to split a complex type into multiple Field stores, and if you need to use lucene to store data, you can use additional column storage. For example, start with "_ l" to represent the stored json array, and "_ m" to represent the stored json object, above which the user object can be split into
User_id= "00000001"
User_name= "test1"
Age=30
Sex=1
Emails= ["test1_1@126.com", "test1_2@126.com"]
Families.children.name= ["son1", "son2"]
Families.children.age= [5,1]
Families.children.sex= [1]
Families.children.birth= ["2013-08-08", "2017-01-01"]
Families.partner.name= "wife"
Families.partner.age=28
Families.partner.sex=2
Families.partner.birth= "1990-01-01"
State= "A"
Create_time=15648784644
Update_time=15648784644
_ mfamilies= "{\" children\ ": [{\" name\ ":\" son1\ ",\" age\ ": 5,\" sex\ ": 1,\" birth\ ":\" 2013-08-08\ "}, {\" name\ ":\" son2\ ",\" age\ ": 1,\" sex\ ": 1,\" birth\ ":\" 2017-01-01\ "],\" partner\ ": {\" name\ ":\" wife\ " \ "age\": 28,\ "sex\": 2,\ "birth\":\ "1990-01-01\"} "
When reading values, field.name () contains "." You can skip it directly, and the field value that starts with _ m is converted to the field value that starts with map,_l and becomes List.