5. solrj and request handler

Source: Internet
Author: User
Tags solr
What is solrj?

Since SOLR exists in the form of a separate webapp, SOLR should provide an API to communicate with SOLR. Right, this is solrj. Since SOLR communicates with each other through a URL, in fact, we do not need solrj, because we can spell strings.

Http: // localhost: 8983/SOLR/artist/select? Q = artist_name: Jay Chou & Wt = JSON & indent = true

This is all possible, but it is more convenient to use solrj to spell strings. If you want to use the string spelling method here, it will be very painful, the biggest benefit of solrj is to parse the returned results. As follows:


{"Responseheader": {"status": 0, "qtime": 2}, "response": {"numfound": 1, "Start": 0, "Docs ": [{"artist_nickname": "Jay Chou", "artist_nameqp": "", "artist_iconno": "0", "artist_mvnum": 191, "artist_authdes ":"", "artist_spaceid":-1, "artist_name": "Jay Chou", "artist_name_exact": "Jay Chou", "artist_categoryid": "847", "artist_songnum, "artist_piclink2": "SOD/artist/560/z/Zhou Jie Lun .jpg", "artist_mobilepic": "SOD/artist/mobile/z/Zhou Jie Lun .jpg", "artist_id": 519, "artist_piclink1": "SOD/artist/14/03/1394692573546.jpg"," artist_pinyininitial ":" zjl "," _ version _ ": 1479005578890051584}]}


If you have to resolve it yourself ..............


Use of solrj Jar package

1. apache-solr-solrj-3.5.0.jar

2. commons-httpclient-3.1.jar

3. slf4j-api-1.6.0.jar

4. commons-logging-1.1.jar


Connect SOLR


HttpSolrServer solr = new HttpSolrServer(            PropertieFactory.getProperty("solr_Song_index_url"));SolrQuery songQuery = new SolrQuery();




Actually, there are two lines of code. The URL must be noted that I use a multi-index library, so the URL is as follows:

Localhost: 8983/SOLR/index database name

In this way, you can connect.




songQuery.setRows(searchParameters.getPageSize());      songQuery.setStart(searchParameters.getStarRecord());      songQuery.setFields("Song_SongID", "Song_Name", "Song_SingerName",            "Song_SpaceID", "Song_FilePic", "Song_FileMV", "Song_FileOL",            "Song_CategoryRelated", "Song_CreateTime",            "Song_ArtistRelated", "Song_UploadFlag", "Song_Quality",            "Song_Attribute", "Song_PlayNum", "Song_DownNum",            "Song_MVPlayNum","Song_ArtistID","Song_CategoryID");      songQuery.setQuery(searchParameters.getSong_Name());      songQuery.setFilterQueries("Song_Attribute:2");



The preceding settings are used to set query parameters. For more information, see solrj wiki.

In fact, you do not need to check the document, because the following forms can be used:






For example, set the query content:

Songquery. Set ("Q", "artist_name: Jay Chou ")


Q is the parameter format in the URL, so we can also see that when solrj is querying, the core is to spell strings ~~

Several common settings are briefly explained. setrows and setstart are natural pages. You can set the number of rows to be returned and the number of records to be returned.

Setfields is the field to be returned

Setquery is used to set query conditions.

Setfilterqueries is used to set filtering conditions. For example, in the above Code, only records with song_attribute 2 are returned.


Parse search results


// Query queryresponse response = SOLR. query (songquery); // The total number of records queried long recordnum = response. getresults (). getnumfound (); // the query result songlist = response. getbeans (COM. search. domain. song. class );



The essence here is that SOLR provides Javabean to receive the returned results. However, the premise is that a bean entity class is required.


@ Fieldprivate integer song_songid; @ field indicates the field to be injected @ field ("song_songid") Public void setsong_songid (integer song_songid) {This. song_songid = song_songid ;}



In the previous Code uninstalling the set method, if you do not want to use the same field name as the SOLR index library in the bean, you can use this annotation to inject song_songid into your own named variables.

It should be noted that the list of object classes is returned when bean is used for receiving.


Is solrj easy to use? :), of course, this is only some basic use.



Request handlers

Each request handler is defined by name and implemented class. You can access the corresponding request handler through the URL. For example

Http: // localhost: 8983/SOLR/collection1/select? Q = SOLR

The request handler whose name is select will be accessed.


Therefore, the role of the request handler is clear. The request handler can be configured to access an index database in a unified manner. In fact, it is similar to the filter, which can be used to configure and pre-process requests in a unified manner, the most typical example is highlighting. See another blog: 6. SOLR highlight and field weight.

It is put together with solrj because the request handler can also configure some common parameters, such as the number of rows returned by each query on the page, which can be configured in the request handler named search. In this way, solrj does not need to be specified separately each time it is accessed, and there are also DIH configurations.

For more handler configurations, see the documentation.

5. solrj and request handler

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.