Elasticsearch Java API Basic Search section

Source: Internet
Author: User

I. Introduction to the version used

Using the elasticsearch2.1.0 version, here is simply an introduction to the search section of the API using

Second, a simple search

When using the API, you can basically write down all the things in the DSL search, giving you the whole process and code for the simplest search, and then just introducing the different searches to the function.

(1) DSL search

For the simplest DSL search, search for a word using a URL to communicate directly, for example, if for a field, search for a specific term or QUERY,DSL as follows:

{"Query": {"term": {"title": "Molong1208 Blog"}}}

The meaning of this search is: in the Title field, the search content is molong1208 blog, the above is the wording of the DSL, in fact, for simple queries, you can also directly use URL query, without JSON format, assuming that we are using the server IP is localhost, For the above query can be written as:

Localhost:9200/index/type/_search? q=title:molong1208 Blog

This is the same as the DSL language above, but this is a simple query to use, such as displaying the desired field, sorting by a field, etc.

Localhost:9200/index/type/_search? q=title:molong1208 Blog&fields=name,title&sort=id:desc&pretty=true

The above URL means to search for content in the title field inside the Index/type, and the fields shown are name and title, sorted in descending order of ID, and the output is formatted in a flattering JSON format

(2) Using the Java API for simple search 1, establishing a connection

Java API when using the search, you must first connect, at the time of the direct URL is Port 9200, but when using the program is 9300, as shown below, to establish a client connection, in the connection class to give the initialization function

[Java]View PlainCopy
  1. Private static Void open ()
  2. {
  3. Settings Settings = Settings.settingsbuilder ()
  4. . put ("Cluster.name", "Molong"). Build ();
  5. try {
  6. Client = Transportclient.builder (). settings (Settings). Build ()
  7. . addtransportaddress (new Inetsockettransportaddress (inetaddress.getbyname ("localhost"), 9300));
  8. } catch (Unknownhostexception e) {
  9. //TODO auto-generated catch block
  10. E.printstacktrace ();
  11. }
  12. }

In this use is the Transportclient connection, also has a nodeclient, did not use, does not introduce here

2. Make inquiries

Query, you need to create a searchrequestbuilder, which will give the index or type to query, and all the settings can be implemented here, such as fuzzy query, range query, prefix query, etc.

[Java]View PlainCopy
    1. Searchrequestbuilder Responsebuilder = Client.preparesearch ("index"). Settypes ("type")

The above code means querying for the type of index, where the client even gets the link, and the next step is to get the query word in.

[Java]View PlainCopy
    1. SearchResponse myresponse=responsebuilder.setquery (Querybuilders.matchphrasequery ("title", "molong1208 Blog "))
    2. . Setfrom (0). setSize.Setexplain (true). Execute (). Actionget ();

The above code is the word to be queried to plug in, where setfrom,setsize refers to the number of pages displayed, starting from the first, showing the size of the data

3. Display

[Java]View PlainCopy
    1. Searchhits hits = Myresponse.gethits ();
    2. for (int i = 0; i < hits.gethits (). length; i++) {
    3. System.out.println (Hits.gethits () [I].getsourceasstring ());}

It can also use Hits.gethits () [I].getsource (), which is a map format that can be displayed in detail

Third, the implementation of other APIs during the search

When we read the DSL, we can see that the query has a lot of queries, such as multi-domain, such as filtering query conditions, the following for the Elasticsearch server development of some basic query DSL given in the Java API implementation of some forms, Many of these forms differ only in the setquery of the above-mentioned plug-in words, so this is just a different function in the story.

(1) Basic Enquiry

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.matchphrasequery ("title", "molong1208 blog"))

The use of the Matchphrasequery (field,text) function, the function of the parameters of two, where the corresponding text portion is to be parsed, for example, molong1208 blog may be parsed after parsing into molong1208 and the blog and then the query

(2) Multi-entry query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.termsquery ("title", "molong1208","blog","Csdn"))

For three words molong1208,blog,csdn in the title field, if any of the three are counted as matching

(3) Match_all query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.matchallquery ())

(4) Common words query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.commontermsquery ("name", "Lishici"))

You can set the specific cutofffrequency in the following

(5) Match query

Use only the Matchphrasequery function, as shown above

(6) Multi_match query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.multimatchquery ("Lishi", "subcat","name"))

Multimatchquery (Text,fields) where fields are the name of the field, you can write several, each of which is separated by commas

(7) Query_string query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.querystringquery (""))

Did not use this query, so the query is not very familiar with the corresponding Elasticsearch server development of 3.3.7, the specific use of the time can be more detailed how to use

(8) Simple_query_string query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.simplequerystringquery (""))

As shown above, 3.3.8

(9) Identifier query

Personal understanding should be similar to the following query, specifically please use the time to study the specific

[Java]View PlainCopy
    1. GetResponse GetResponse = Client.prepareget ("Users", " user", "3"). get ();

(10) Prefix query

[JavaScript]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.prefixquery ("title", "Mo"))

The previous parameter is the prefix word used after field one argument for

(one) Fuzzy_like_this,fuzzy_like_this_field,fuzzy query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.fuzzyquery ("title", "Malong"))

In the version used, did not find how to use the Fuzzy_like_this and Fuzzy_like_this_field functions, do not know whether there is this function, only found that there is fuzzy query, can be set after the boost equivalent

(12) Wildcard query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.wildcardquery ("title", "Molo?g"))

(More_like_this,more_like_this_field)

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.morelikethisquery (). Addliketext ("Long"))
[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.morelikethisquery ("Long"))

There are two methods, the second one is in the _all range of queries, the first after a lot of can be set, there is a need to use the specific reference

(rang) query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.rangequery ("age"). GT (Ten). LT (+))

For a field, greater than, how much less

(Dismax) Inquiry

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.dismaxquery (). Add (Querybuilders.termquery ("title", "molong1208") ))  

(16) Regular expression query

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.regexpquery (field, regexp))

Four, BOOL Query

The above is just about the specific query method, sometimes we want to be more complex queries, such as to find a field must have a value, and another field must have another value, this time can use BOOL query, such as the following

[Java]View PlainCopy
    1. Responsebuilder.setquery (Querybuilders.boolquery (). Must (Querybuilders.multimatchquery (query, "name"," Title "," Title_1 ")). Must (Querybuilders.multimatchquery (Query2, " Title2 "," Title3 " )))

The above means that there is a query in the title or Title_1 or Name field, and there is a query2 result recall in the Title2 or Title3 field

Of course, according to their own situation, there are should,must_not and other options

Five, filter

When using filtering, version 2.1.0 has only postfilter, for example

[Java]View PlainCopy
    1. Responsebuilder.setpostfilter (Querybuilders.existsquery ("title"))

The exists filter, the filter with the Title field, is understood by the individual and may be wrong

[Java]View PlainCopy
    1. Responsebuilder.setpostfilter (Querybuilders.missingquery ("title"))

The meaning of this is completely different from the above, you can refer to the Elasticsearch Server development Specific section of the introduction

and postfilter for the results of the filter is the result of the search, the results are filtered, can be understood as a post-filter, ES most of the first filter after the aggregation, this can be set to first aggregation after filtering

Elasticsearch Java API Basic Search section

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.