SOLR creates indexes and solr indexes for multiple (associated) tables.

Source: Internet
Author: User

SOLR creates indexes and solr indexes for multiple (associated) tables.

I haven't written a blog for another two days. I'm also drunk when I have been writing SOLR for a week. After all, there are not many things. I didn't write any overtime work on Thursday evening, my colleagues at the company did not talk about this at a meeting on Friday evening. I am at home today. I will write the last one. The rest of my work is to create an index for multiple tables. tables can be associated, it can also be unrelated. Start later.

1. Unrelated multi-Table creation index 1.1 database preparation

First, create a table in the database. There is already a tuser table. I have created another role table with two or three fields. There is nothing special about this, then you can add a few pieces of data to it. Let's take a look at the database:

1.2data-config. xml

After the database is created, it is to modify the data-config.xml file, because the association between the database and solr is mainly built on the file, so the next is to modify the file, I have already written, paste the following directly to see it:

You can add a <entity> label in the middle of the <document> </document> label, and then add the corresponding field to the middle. However, pay attention to the following points:

Note:

1. the name attribute of the <entity> tag, which is used to distinguish different entity, so it cannot be the same as the name of other entity tags.

2. What is the primary key of the table in the database, and what is the value of the primary key attribute cannot be changed.

3. the third point is very important. First, we will try schema again. this attribute is configured in xml. This configuration indicates that the id value is unique When solr creates an index. It cannot be repeated just like the primary key of a database table, an error is reported when primary keys are repeated in the database. However, if the value of id is the same When solr creates an index, the index created later will overwrite the index created earlier, when we create a table, the primary key IDs are generally int-Type Auto-incrementing. In this way, the IDs of the two tables are likely to be the same, so the index overwrites will occur. How can this problem be solved? One way is to use the uuid for the primary key id. This will certainly not repeat, so that the index will not be overwritten. However, in most cases, we still use the auto-increment method as the id. I checked it online and someone directly went to the schema. remove the uniqueKey attribute from xml, and create a new field in the two tables. Set this fieldUniqueKeyAttribute value, so as to achieve the purpose of not covering the index, but I have not tried these two methods. If you are interested, try it on your own.

4. you can configure multiple dataSource in this file. That is to say, if two tables are not in the same database, you can create indexes for the two tables at the same time, you only need to map the table to the data source as follows:

1. 3. Modify schema. xml

Add the fields in the table that need to be indexed to this file. Note that,You do not need to add the same fields in the two tables.For example, if both tables have IDs, you can write the id field only once.

After completing the preceding configuration, start tomcat, access the localhost: 8080/solr page, and recreate the index. The created index is as follows:

{"ResponseHeader": {"status": 0, "QTime": 1, "params": {"indent": "true", "q ":"*: * "," _ ":" 1445664391222 "," wt ":" json "}}," response ": {" numFound ": 8," start ": 0, "docs": [{"userAge": 33, "userAddress": "China", "isdelete": 0, "updateTime": "2015-10-19T12: 41: 09Z ", "id": 43, "userName": "Andy Lau", "_ version _": 1515741213715595300 },{ "userAge": 44, "userAddress": "China ", "isdelete": 0, "updateTime": "2015-10-19T12: 41: 26Z", "id": 44, "userName": "Stephen Chow", "_ version _": 1515741214565990400 },{ "userAge": 55, "userAddress": "China", "isdelete": 0, "updateTime": "2015-10-19T12: 41: 44Z", "id ": 45, "userName": "Liu ruoying", "_ version _": 1515741214565990400 },{ "userAge": 123, "userAddress": "123", "isdelete": 0, "updateTime": "2015-10-19T12: 51: 30Z", "id": 46, "userName": "Zhou runfa", "_ version _": 1515741214565990400 },{ "userAge ": 456, "userAddress": "456", "isdelete": 0, "updateTime": "2015-10-19T12: 51: 40Z", "id": 47, "userName ": "Liang chaowei", "_ version _": 1515741214565990400}, {"id": 1, "RoleDiscrib": "I am the boss", "RoleName": "Administrator ", "_ version _": 1515741214598496300 },{ "id": 2, "RoleDiscrib": "I'm boss 1", "RoleName": "Administrator 1 ", "_ version _": 1515741214598496300 },{ "id": 3, "RoleDiscrib": "I am the boss 12", "RoleName": "Administrator 12 ", "_ version _": 1515741214598496300}]}View Code

There are only eight data records in two tables in my database, and eight indexes are created. For IDS, as long as the IDs of the two tables are not repeated, no index overwrites will occur, so here is just a simple example. You can follow the methods I mentioned above for indexing and overwriting. Of course there are better methods.

 

2. Create an index for the associated table

As for how to create an index for an associated table, it is similar to creating an index for a single table. I originally wanted to create several tables separately and then write them, but I suddenly remembered that a friend gave me a comment two days ago saying that I couldn't understand what I wrote. So I want to use the official example this time. What do I mean? You just need to paste the content in the official document to try it. In fact, the official document is better written.

2. 1. Database Configuration

  

2.2.data-congfig. xml configuration <dataConfig> <dataSource driver = "org. hsqldb. jdbcDriver "url =" jdbc: hsqldb: /temp/example/ex "user =" sa "/> <document name =" products "> <entity name =" item "query =" select * from item "> <field column = "ID" name = "id"/> <field column = "NAME" name = "name"/> <field column = "MANU" name = "manu"/> <field column = "WEIGHT" name = "weight"/> <field column = "PRICE" name = "price"/> <field column = "POPULARITY" name = "popularity" /> <field column = "INSTOCK" name = "inStock"/> <field column = "INCLUDES" name = "includes"/> <entity name = "feature" query =" select description from feature where item_id = '$ {item. ID} '"> <field name =" features "column =" description "/> </entity> <entity name =" item_category "query =" select CATEGORY_ID from item_category where item_id = '$ {item. ID} '"> <entity name =" category "query =" select description from category where id =' $ {item_category.CATEGORY_ID} '"> <field column =" description "name =" cat "/> </entity> </document> </dataConfig>View Code

In addition to the above method, the official document also provides another method, but I have never used this method. Below I will post the method and also post the explanation of the official document, you can use it on your own, but I still like to use the above method:

<DataConfig> <dataSource driver = "org. hsqldb. jdbcDriver "url =" jdbc: hsqldb: /temp/example/ex "user =" sa "/> <document> <entity name =" item "query =" select * from item "> <entity name =" feature" query = "select description as features from feature where item_id = '$ {item. ID} '"/> <entity name =" item_category "query =" select CATEGORY_ID from item_category where item_id =' $ {item. ID} '"> <entity name =" category "query =" select description as cat from category where id =' $ {item_category.CATEGORY_ID} '"/> </entity> </document> </dataConfig>View Code

The official explanation is as follows:

In the above example, there are mappings of fields to Solr fields. it is possible to totally avoid the field entries in entities if the names of the fields are same (case does not matter) as those in Solr schema. you may need to add a field entry if any of the built-in Tranformers are used (see Transformer section)

 

Next, modify the schema. xml document and put the fields to be indexed in the file.

 

I have already introduced how to create indexes for multiple tables. Although the writing is rough, you should try again. For more information, refer to the official documentation for reference.

In addition, I will not write any other things about solr for the time being. On the one hand, it is because I have not studied this for a long time, and I am not very familiar with some things. Currently, I can only get started. On the other hand, the project that was originally used to use solr is no longer needed, and the new project is also urgent, so we have to put down things about solr for the time being, so we can continue to use it later. Well, this is now.

 

 

 

 

 

  

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.