In Elasticsearch, you can search using the _search terminal. This was described a lot in my previous article “Getting Started with Elasticsearch (2)”. For this kind of search, we can use a powerful DSL to search. Another category of Elasticsearch is URI-based search. For this purpose it is convenient to use directly in the browser address bar or on the command line. Not all search options are public when performing searches using this pattern, but it can be handy for quick “curl tests.” In today’s article, we will make a simple description. I also need to point out that the syntax is the same as the Search Bar syntax in Kibana.

 

Install the Elastic Stack

If you don’t already have Elasticsearch and Kibana installed, see the article “Elastic: A Beginner’s Guide” to install your own Elasticsearch and Kibana.

 

Prepare the data

To illustrate, we first created our Twitter index in Kibana using the following bulk directive.

POST _bulk { "index" : { "_index" : "twitter", "_id": 1}} {"user":" Zhang SAN ","message":" Nice weather today, Walk to ", "uid" : 2, "age" : 20, "city", "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "haidian district in Beijing, China", "location" : {" lat ":" 39.970718 ", "l On ":" 116.325747 "}, "DOB" : "1980-12-01"} {" index ": {" _index" : "twitter", "_id" : 2}} {"user":" Lao Liu ","message":" Start, next stop Yunnan!" , "uid" : 3, "age" : 30, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China Beijing dongcheng district stylobate factory three 3", "location" : {" lat ":" 39.904313 "," Lon ":" 116.412754 "}, "DOB" : "1981-12-01"} {" index ": {" _index" : "twitter", "_id" : 3}} {"user":" message":"happy birthday!" , "uid" : 4, "age" : 30, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China Beijing dongcheng district", "location" : {" lat ":" 39.893801 ", "says lon" : "1 16.408986 "}, "DOB" : "1982-12-01"} {" index ": {" _index" : "twitter", "_id" : 4}} {" user ", "old jia", "message" : "123, gogogo", "uid" : 5, "age" : 35, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China Beijing chaoyang district jianguomen", "Location" : {" lat ":" 39.718256 ", "says lon" : "116.367910"}, "DOB" : "1983-12-01"} {" index ": {" _index" : "twitter", "_id" : 5}} {"user":" Lao Wang ","message":"Happy BirthDay My Friend!" , "uid" : 6, "age" : 50, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "chaoyang district in Beijing, China international trade", "location" : {" lat ":" 39.918256 ", "says lon" : "116.467910"}, "DOB" : "1984-12-01"} {" index ": {" _index" : "twitter", "_id" : 6}} {"user":" Lao Wu ","message":" today is my birthday, friends come, what birthday happy!" , "uid" : 7, "age" : 90, the "city" : "Shanghai", "province", "Shanghai", "country" : "Chinese", "address" : "China Shanghai minhang district", "location" : {" lat ":" 31.175927 ", "says lon" : "1 21.383328 "}, "DOB" : "1985-12-01"}Copy the code

There are six pieces of data.

Now, let’s do some query actions.

Search data

First, let’s do a simple search. We can type the following command into the browser:

GET twitter/_search? Q = user: zhang SANCopy the code

We query the document we need through “q=user: Zhang SAN”. Sometimes this is a very fast query. We can also type a URI like this in the browser:

http://localhost:9200/_search?q=user:%E5%BC%A0%E4%B8%89&pretty
Copy the code

Or on the command line:

Below, we’ll use Kibana to show some of the most basic features of using URI search.

URI queries use syntax to parse AND split the supplied query string based on operators such as OR, AND, OR NOT

We want to use sort to sort the data:

GET twitter/_search? Q = city: "Beijing" & sort = DOB: descCopy the code

It shows all users from Beijing in descending order of date of birth.

If we only want to display age, DOB, and city information in _source, we can do this:

GET twitter/_search? Q = city: "Beijing" & sort = DOB: desc & _source = city, age, DOBCopy the code

As you can see from the above display, we only see three fields displayed. Let’s say we want to paginate and only have 2 documents per page, so we can do this:

GET twitter/_search? Q = city: "Beijing" & sort = DOB: desc & _source = city, age, DOB&size = 2Copy the code

As you can see from the display above, only two documents are displayed even though there are a total of five documents that meet the criteria.

If we want to count all users whose city is “Shanghai” and “Beijing”, we can use the following statement:

GET twitter/_search? Q =city:(" Beijing "or" Shanghai ") &sort= desc&_source=city,age,DOB&size=2Copy the code

Obviously at this point, we’ve got six pieces of data. All users in Shanghai and Beijing were searched.

If we want to query a document from “Beijing” with the name “Zhang SAN”, we can query it like this:

GET twitter/_search? Q =city:" Beijing "AND user:" Zhang SANCopy the code

You can see from the top that there is only one piece of data.

If we want to get all the users except Shanghai, we can use the following method to get:

GET twitter/_search? Q =NOT city:" Shanghai"Copy the code

We see five data points.

We can also weight some ideas so that they are ranked higher, for example:

The above query is looking for someone aged 20, or from Shanghai. From the search results, we can see that Shanghai’s Lao Wu is ranked first. If we want to pay more attention to people aged 20, we can weight their search results so that they score higher. We can do it in the following way:

GET twitter/_search? Q =(age:20^5 OR city:" Shanghai ")Copy the code

Above, we’ve obviously weighted the option whose age is 20. Then the search result is:

We can see that Zhang SAN, whose age is now 20, is at the top of the search results.

If we do not specify any fields, the search will be performed on all fields:

GET twitter/_search? Q = zhang SANCopy the code

Of course, we can also do fuzzy search:

It indicates that an Edit error can also be searched. For Chinese retrieval, this relies on the word divider. No specific word dividers were used in our experiment. There may be a difference between this and actual use.

We can also search the following ranges:

GET twitter/_search? q=age:[20 TO 30]Copy the code

The above search results are for all results from 20 to 30 years old, and are included in it. If we didn’t want to include age 30, we could write it like this:

We use [20 TO 30]. If we want TO search all documents under the age of 30, we can use the following search method:

Here, we use [* TO 30}, which does not include 30.

Okay, that’s it for today. All of the syntax here also applies to Search Bar in Kibana. If we master these proficiently, it is also very convenient for us to skillfully operate Kibana search.

 

Reference:

【 1 】 www.elastic.co/guide/en/el…

(2) www.elastic.co/guide/en/el…