Pinned queries are used to increase the ranking of selected documents above those that match a given query. This feature is typically used to direct searchers to select documents that are promoted over any “Organic” matches in the search. Use the document ID stored in the _ID field to identify the upgraded or “fixed” document.

 

Here is an example to illustrate:

To prepare data

Import Elasticsearch data into Elasticsearch using the bulk API as follows:

POST _bulk { "index" : { "_index" : "twitter", "_id": 1}} {"user":" Zhang SAN ","message":" Nice weather today, Walk to, "" uid" : 2, "city", "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "haidian district in Beijing, China", "location" : {" lat ":" 39.970718 ", "says lon" : "116. 325747 "}, "DOB" : "1980-12-01"} {" index ": {" _index" : "twitter", "_id" : 2}} {" user ":" liu ", "message" : "in yunnan, the next stop!" , "uid" : 3, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China Beijing dongcheng district stylobate factory three 3", "location" : {" lat ":" 39.904313 ", "says lon" : "116 . 412754 "}, "DOB" : "1981-12-01"} {" index ": {" _index" : "twitter", "_id" : 3}} {" user ":" bill ", "message" : "happy birthday!" , "uid" : 4, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China Beijing dongcheng district", "location" : {" lat ":" 39.893801 ", "says lon" : "116.408986 "}, "DOB":"1982-12-01"} { "index" : { "_index" : "twitter", "_id": 4}} {" user ", "old jia", "message" : "123, gogogo", "uid" : 5, "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, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "chaoyang district in Beijing, China international trade", "location" : {" lat ":" 39.918256 ", "says lon" : "116.4679 10"}, "DOB":"1984-12-01"} { "index" : { "_index" : "twitter", "_id": 6}} {"user":" Lao Wu ","message":" today is my birthday, friends come, what birthday happy!" , "uid" : 7, "city", "Shanghai", "province", "Shanghai", "country" : "Chinese", "address" : "China Shanghai minhang district", "location" : {" lat ":" 31.175927 ", "says lon" : "121.383328 "}, "DOB":"1985-12-01"}Copy the code

So we have six pieces of data. They have ids from 1 to 6.

 

search

The normal search

First let’s do a normal search, such as finding all documents in Beijing:

GET twitter/_search {"query": {"match": {"city. Keyword ": "keyword"}}}Copy the code

Then the query result is:

"Hits" : [{" _index ":" twitter "and" _type ":" _doc ", "_id" : "1", "_score" : 0.24116206, "_source" : {" user ": "Zhang", "message" : "today the weather is good, walk to", "uid" : 2, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "Haidian district in Beijing, China", "location" : {" lat ":" 39.970718 ", "says lon" : "116.325747"}, "DOB" : "1980-12-01"}}, {" _index ": "Twitter," "_type" : "_doc", "_id" : "2", "_score" : 0.24116206, "_source" : {" user ":" liu ", "message" : ", "uid" : 3, "city" : "Beijing ", "province" :" Beijing ", "country" : "China ", "address" :" No.3 taiji Chang 3, Dongcheng District, Beijing, China ", "location" : {" lat ":" 39.904313 ", "says lon" : "116.412754"}, "DOB" : "1981-12-01"}}, {" _index ":" twitter "and" _type ": "_doc", "_id" : "3", "_score" : 0.24116206, "_source" : {" user ":" bill ", "message" : "happy birthday!", "uid" : 4, "city", "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China Beijing dongcheng district", "location" : {" lat ": "39.893801", "says lon" : "116.408986"}, "DOB" : "1982-12-01"}}, {" _index ":" twitter "and" _type ":" _doc ", "_id" : "4", "_score" : 0.24116206, "_source" : {" user ":" old jia ", "message" : "123, gogogo", "uid" : 5, "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 ":" twitter "and" _type ":" _doc ", "_id" : "5", "_score" : 0.24116206, "_source" : {"user" : "old king ", "message" : "Happy BirthDay My Friend ", "uid" : 6, "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"}Copy the code

We can see that there are five data results, and their _id is from 1 to 5.

So the question is: if I want to rank the two documents with the ids 4 and 5 at the top of the query, how do I do that? The answer is that we use pinned query.

Pinned query

We can use the following way to query:

GET twitter / _search {" query ": {" pinned" : {" ids: "/" 4 ", "5", "organic" : {" match ": {" city. The keyword" : "Beijing"}}}}}Copy the code

So let’s look at our query result:

"Hits" : [{" _index ":" twitter "and" _type ":" _doc ", "_id" : "4", "_score" : 1.7014124 e38, "_source" : {" user ": "Lao jia", "message" : "123, gogogo", "uid" : 5, "city" : "Beijing", "province", "Beijing", "country" : "Chinese", "address" : "China's Beijing chaoyang district jianguomen", "location" : {" lat ":" 39.718256 ", "says lon" : "116.367910"}, "DOB" : "1983-12-01"}}, {" _index ": "Twitter," "_type" : "_doc", "_id" : "5", "_score" : 1.7014122 e38, "_source" : {" user ":" wang ", "message" : "City" : "Beijing ", "province" :" Beijing ", "country" : "China ", "address" : "Chaoyang district in Beijing, China international trade", "location" : {" lat ":" 39.918256 ", "says lon" : "116.467910"}, "DOB" : "1984-12-01"}}, {" _index ": "Twitter," "_type" : "_doc", "_id" : "1", "_score" : 0.24116206, "_source" : {" user ":" zhang ", "message" : "City" : "Beijing ", "province" :" Beijing ", "country" : "China ", "address" :" Haidian district, Beijing ", "location" : {" lat ":" 39.970718 ", "says lon" : "116.325747"}, "DOB" : "1980-12-01"}}, {" _index ":" twitter "and" _type ": "_doc", "_id" : "2", "_score" : 0.24116206, "_source" : {" user ":" liu ", "message" : "in yunnan, the next stop!", "uid" : 3, "city" : "Beijing ", "province" :" Beijing ", "country" : "China ", "location" : {"lat" : "39.904313"," LON ": {"lat" : "39.904313"," LON ": "116.412754"}, "DOB" : "1981-12-01"}}, {" _index ":" twitter "and" _type ":" _doc ", "_id" : "3", "_score" : 0.24116206, "_source" : {"user" : "user", "message" : "happy birthday!", "uid" : 4, "city" : "Beijing ", "province" : "Beijing", "country" : "Chinese", "address" : "China Beijing dongcheng district", "location" : {" lat ":" 39.893801 ", "says lon" : "116.408986"}, "DOB" : "1982-12-01"}}]Copy the code

In this query result, we can see that the two documents whose ids are 4 and 5 are ranked first and their scores have been improved.

 

Reference:

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