“prefix”
query matches documents that have fields containing terms with a specified
prefix.
Add mappings
to type “titles” like below.
PUT /books/_mappings/titles
{
"titles" : {
"properties" : {
"title" :{
"type" : "string",
"index": "not_analyzed"
}
}
}
}
Insert below
data into type “titles”.
PUT /_bulk
{"create" : {"_index": "books", "_type": "titles", "_id": "1" }}
{"id" : 1, "title" : "Courage: The Joy of Living Dangerously"}
{"create" : {"_index": "books", "_type": "titles", "_id": "2" }}
{"id" : 2, "title" : "Creativity: Unleashing the Forces Within"}
{"create" : {"_index": "books", "_type": "titles", "_id": "3" }}
{"id" : 3, "title" : "Joy: The Happiness That Comes from Within"}
{"create" : {"_index": "books", "_type": "titles", "_id": "4" }}
{"id" : 4, "title" : "Freedom: The Courage to Be Yourself "}
{"create" : {"_index": "books", "_type": "titles", "_id": "5" }}
{"id" : 5, "title" : "The Book of Secrets"}
{"create" : {"_index": "books", "_type": "titles", "_id": "6" }}
{"id" : 6, "title" : "Love, Freedom, and Aloneness: The Koan of Relationships"}
{"create" : {"_index": "books", "_type": "titles", "_id": "7" }}
{"id" : 7, "title" : "Life, Love, Laughter: Celebrating Your Existence"}
Get all
books whose title starts with ‘L’.
GET /books/titles/_search
{
"query":{
"prefix": {
"title": "L"
}
}
}
You will get
following response.
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "books",
"_type": "titles",
"_id": "6",
"_score": 1,
"_source": {
"id": 6,
"title": "Love, Freedom, and Aloneness: The Koan of Relationships"
}
},
{
"_index": "books",
"_type": "titles",
"_id": "7",
"_score": 1,
"_source": {
"id": 7,
"title": "Life, Love, Laughter: Celebrating Your Existence"
}
}
]
}
}
No comments:
Post a Comment