Elasticsearch Search Type Introduction

Source: Internet
Author: User

Simple search

GETThe request is very simple-you can easily get the document you want. Let's try something further, like a simple search!

We try one of the simplest search requests for all employees:

GET /megacorp/employee/_search

Next, let's search for employees whose last name contains "Jake" . To do this, we'll use a lightweight search method on the command line. This method is often referred to as a query string Search because we pass a query statement like a URL parameter:

GET /megacorp/employee/_search?q=last_name:jake

We still use the keyword in the request _search , and then pass the query statement to the parameter q= . This will give you the result of all the last names of Jake.

Querying with a DSL statement

Query string search facilitates the completion of a specific (ad hoc) search through the command line, but it also has limitations (see the Simple search section). Elasticsearch provides a rich and flexible query language called DSL queries (query DSL), which allows you to build more complex and powerful queries.

DSL (Domain Specific language-specific domain language) appears as a JSON request body. We can say this before about the "Jake" query:

GET /megacorp/employee/_search{    "query" : {        "match" : {            "last_name""jake"        }    }}

This returns the same results as the previous query. You can see that something has changed, instead of using the query string as a parameter, instead of using the request body. The request body is expressed in JSON, which uses a match statement (one of the query types, which we will learn later).



More complex searches

We make the search a little bit more complicated. We still want to find employees whose surname is "Jake", but we only want to get employees older than 30 years old. Our statement will add filter , which allows us to perform a structured search efficiently:

get/megacorp/employee/_search{ "query" : { "filtered" : {" filter ": {: { : {" GT ": 30 } <1  >}},  "query" : { "match" : {: " Jake " <2  >}}}}  
    • <1> This part of the query belongs to the interval filter (range filter), which is used to find all data older than 30 years old-the gt abbreviation for "Greater than".
    • <2> This part of the query is consistent with the previous match statement (query) .

Phrase Search

We can now search for a single word in the field, which is good, but sometimes you want to match several words or phrases exactly (phrases). For example, we want to query employee records that contain both "rock" and "climbing" (and are adjacent).

To do this, we simply match change the query to a match_phrase query:

GET /megacorp/employee/_search{    "query" : {        "match_phrase" : {            "about""rock climbing"        }    }}
Highlight our Search

a lot of apps like to Highlight (highlight) to match the keyword so that users can know why these documents and queries match. It is very easy to highlight clips in the Elasticsearch.

Let's add highlight parameter on the previous statement. When we run this statement, we will hit the same result as before, but there will be a new part in the returned result called <EM></EM>

Ps:web How to do highlighting, you can refer to: http://www.w3school.com.cn/tags/tag_phrase_elements.asp

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Elasticsearch Search Type Introduction

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.