SOLR test Project (bottom)--index data and query

Source: Internet
Author: User
Tags commit object model solr

Then SOLR test Project (middle) – integrated Spring-data-mongo
and SOLR test Project (top) – Maven-based SPRINGMVC environment setup

Start importing data and implementing query Schema API today

If fields are not defined, then all fields are array types []

So the first step is to create fields from the object model, where the schema API is used, and the types are simple.

After the fields have been created, the new data

Process

problems that exist

When creating fields with the schema API is not a Chinese word segmentation, resulting in the query will be all the Chinese according to the Chinese character one by one, as long as the match to the query to any one is queried

Query "Cat and mouse", the correct query results should be only one, and the actual query in addition to 3, if the custom field query such as name: Cat and mouse, then can only be exactly match, like this name is "cat and mouse Comedy essence version" can't match to

Solution Solutions

Reference Solr6.1 configuration Chinese word breaker
Chinese word segmentation is configured, and a fieldtype is defined as Text_ik

The first step is to create a field

Interface Address

Http://localhost:8983/solr/wechat/schema

Request Method: POST
Request message:

{"
    Add-field": {"name":
        "name",
        "type": "Text_ik"
    },
    "Add-field": {
        "name": "Actors",
        "Type": "Text_ik"
    },
    "Add-field": {
        "name": "description",
        "type": "Text_ik"
    },
    " Add-field ": {
        " name ":" directors ",
        " type ":" Text_ik "
    }
}

Rebuilding indexes

Now name: Cat and mouse can be found, if there is no Chinese word, name must be cat and mouse comedy essence version

Let's test the appearance of no participle.

Create a new Testcore

Use the schema API to create fields, because the array type is not created.

Interface Address

Http://localhost:8983/solr/testcore/schema

Request Method: POST
Request message:

{"
    Add-field": {"name":
        "name",
        "type": "String"
    },
    "Add-field": {
        "name": "Actors",
        " Type ":" String "
    },
    " Add-field ": {
        " name ":" description ",
        " type ":" String "
    },
    " Add-field ": {
        " name ":" directors ",
        " type ":" String "
    },
    " Add-field ": {
        " name ":" Showtime ",
        " Type ":" "string"
    },
    "Add-field": {
        "name": "Imgurl",
        "type": "String"
    },
    "Add-field": {
        "name": "Playcount",
        "type": "int"
    },
    "Add-field": {
        "name": "Userscore",
        "type": " int "
    },
    " Add-field ": {
        " name ":" Imguistyle ",
        " type ":" int "
    }
}

Next we import the data, pour the data and look at a specific piece of data

No Chinese participle

Solrquery query = new Solrquery ();
Query.setquery ("Name: Cat and Mouse");
Queryresponse RSP = client.query (query);
Solrdocumentlist docs = Rsp.getresults ();
System.out.println (docs);
Query.setquery ("Name: Cat and Mouse Comedy Essence Edition");
Queryresponse RSP1 = client.query (query);
Solrdocumentlist DOCS1 = Rsp1.getresults ();
System.out.println (DOCS1);

No configuration participle must match exactly

If a word breaker is configured, you can find

Summary

From beginning to end, I tested SOLR, and summed up the MAVEN,SPRING,MONGO,SOLR with Solrj tool class

Import org.apache.solr.client.solrj.SolrClient;
Import Org.apache.solr.client.solrj.beans.DocumentObjectBinder;
Import org.apache.solr.client.solrj.impl.HttpSolrClient;

Import org.apache.solr.common.SolrInputDocument;
    public class Solrjtest {private static solrclient client;
    private static String URL;
        static {URL = "Http://localhost:8983/solr/wechat";
    Client = new Httpsolrclient.builder (URL). build (); /** * Save or UPDATE SOLR data * * @param res */public static <T> Boolean savesolrresource (T so
        lrentity) {Documentobjectbinder binder = new Documentobjectbinder ();
        Solrinputdocument doc = binder.tosolrinputdocument (solrentity);
            try {Client.add (doc);
        Client.commit ();
            } catch (Exception e) {e.printstacktrace ();
        return false;
    } return true; }/** * Delete SOLR data * * @param ID */public static Boolean reMovesolrdata (String id) {try {Client.deletebyid (ID);
        Client.commit ();
            } catch (Exception e) {e.printstacktrace ();
        return false;
    } return true;
 }

}

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.