ElasticSearch Java API-Retrieving index libraries

Source: Internet
Author: User
Tags log4j

The previous blog recorded how to write data to an index using the Java Call API, and how to search this time.

First, prepare the data
    StringData1 = Jsonutil.model2json (NewBlog (1,"Introduction to Git","2016-06-19","The main difference between SVN and git ..."));StringData2 = Jsonutil.model2json (NewBlog (2,"Introduction and simple use of generics in Java","2016-06-19","Learning objectives to master the meaning of generics ..."));StringData3 = Jsonutil.model2json (NewBlog (3,"SQL basic Operations","2016-06-19","Basic operation: CRUD ..."));StringDATA4 = Jsonutil.model2json (NewBlog (4,"Hibernate Framework Basics","2016-06-19","Hibernate framework Basics ..."));StringData5 = Jsonutil.model2json (NewBlog (5,"Git Basics git","2016-06-19","What the shell is ..."));StringDATA6 = Jsonutil.model2json (NewBlog (6,"Basic knowledge of C + +","2016-06-19","What the shell is ..."));StringData7 = Jsonutil.model2json (NewBlog (7,"MySQL Basics","2016-06-19","What Git is ..."));
Second, query

A single query can be divided into the following four steps:

  • 1. Create a client that connects to the Elasticsearch service.
    Index on the Elasticsearch server, the first step is to create a connection to the server.

    Client client = TransportClient.builder().build()            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
  • 2. Create a querybuilder.
    QueryBuilder you can set a query for a single field, or you can set up multiple fields.
    e.g.1: Query hibernate The document containing the keyword in the title field:

    QueryBuilder qb1 = termQuery("title", "hibernate");

    e.g.2: Queries the document that contains the keyword in the title field or the Content field git :

    QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
  • 3. Execute the query
    Set the index, type, and query of the queries through the client. Returns a SearchResponse object:

    SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()            .actionGet();
  • 4. Working with query Results
    The Gethits () method of the SearchResponse object gets the query results, returns a collection of Searchhits, and iterates through the collection to get the document information for the query:

    SearchHits hits = response.getHits();
Third, Java code implementation
Package CN. com. Bropen. Es;Import Java. NET. InetAddress;Import Java. NET. Unknownhostexception;import org. Elasticsearch. Action. Search. SearchResponse;import org. Elasticsearch. Client. Client;import org. Elasticsearch. Client. Transport. Transportclient;import org. Elasticsearch. Common. Transport. Inetsockettransportaddress;import org. Elasticsearch. Index. Query. Multimatchquerybuilder;import org. Elasticsearch. Index. Query. QueryBuilder;import org. Elasticsearch. Index. Query. Querybuilders;import org. Elasticsearch. Search. Searchhit;import org. Elasticsearch. Search. Searchhits;Import static Org. Elasticsearch. Index. Query. Querybuilders.*;public class Elasticsearchget {public static void main (string[] args) {//client startup try { Client client = Transportclient. Builder(). Build(). Addtransportaddress(New Inetsockettransportaddress (inetaddress. Getbyname("127.0.0.1"),9300));QueryBuilder QB1 = Termquery ("title","Hibernate");QueryBuilder qb2= Querybuilders. Multimatchquery("Git","title","Content");SearchResponse response = Client. Preparesearch("Blog"). Settypes("article"). Setquery(QB2). Execute(). Actionget();Searchhits hits = response. Gethits();if (hits. Totalhits() >0) {for (Searchhit hit:hits) {System. out. println("Score:"+hit. Getscore()+": \ T"+hit. GetSource());//. Get ("title")}} else {System. out. println([0 results found ]);}} catch (Unknownhostexception e) {E. Printstacktrace();}    }}

Query Result:

Log4j:warn No appenders could be found forLogger (org.elasticsearch.plugins). Log4j:warn Initialize the log4j system Properly.log4j:WARN See/HTTP logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Score:0.5: {posttime= .- .- +, id=1, Title=git Introduction, the main difference between CONTENT=SVN and git...}score:0.17673586: {posttime= .- .- +, id=7, Title=mysql basic knowledge, content=git is what...}score:0.049935166: {posttime= .- .- +, id=5, Title=git basic knowledge of git, Content=shell is what...}

Resources:

    1. Distributed Search Elasticsearch Java API (v)--Search
    2. GET API
    3. Elasticsearch Chinese Guide –java-api

ElasticSearch Java API-Retrieving index libraries

Related Article

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.