SOLR Note 4-General steps to create an index and search for solrj

Source: Internet
Author: User
Tags solr

General steps for creating an index in solrj:

1) create a solrserver object, which is used to manage indexes.

2) create a solrinputdocument object, that is, a document object, and add fields to the Document Object.

3) use the Add method of the solrserver object to add the solrinputdocument object and create an index.

4) Call The COMMIT () method of the solrserver object to submit the index.

For example:

Httpsolrserver HSS = new httpsolrserver ("http: // localhost: 8080/SOLR ");

Solrinputdocument = new solrinputdocument ();
Solrinputdocument. addfield ("ID", 1 );
Solrinputdocument. addfield ("uname", "zhangsan ");
Solrinputdocument. addfield ("sex", sex. Male. tostring ());
Solrinputdocument. addfield ("Age", 1 );

Solrinputdocument. addfield ("holobby", "basketball ");

HSS. Add (solrinputdocument );

HSS. Commit ();


General Search Steps:

1) create a solrserver object

2) create a query statement solrquery object

3) create a solrrequest object

4) generate a solrresponse object through the solrrequest process (solrserver) Method

5) Call the getresults () method of the solrresponse object to obtain the result set.

Here we can also directly create a solrresponse object through solrserver's query (solrquery.

For example:

1. httpsolrserver HSS = new httpsolrserver ("http: // localhost: 8080/SOLR ");

Solrquery sq = new solrquery ("ID: 1 ");

Queryrequest QR = new queryrequest (SQ );

Queryresponse q = QR. Process (HSS );

Solrdocumentlist list = Q. getresults ();

Solrdocument SD = list. Get (0 );

System. Out. println (SD. Get ("uname "));

2. httpsolrserver HSS = new httpsolrserver ("http: // localhost: 8080/SOLR ");

Solrquery sq = new solrquery ("*:*");

Queryresponse QR = HSS. Query (SQ );

Solrdocumentlist list = QR. getresults ();

For (solrdocument SD: List)
{
Iterator iter = SD. keyset (). iterator ();
For (; ITER. hasnext ();)
{
String key = (string) ITER. Next ();
Object value = SD. Get (key );

System. Out. println (Key + "---->" + value );


}
System. Out. println ("----------------------------");
}

SOLR Note 4-General steps to create an index and search for solrj

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.