loopback Custom order by - angularjs

I am using angularjs for frontend and loopback for backend and elastic search for the database.
I have a model with properties as:
"name": {
"type": "string",
"required": true
},
"mobileNumber": {
"type": "string",
"required": true
},
"email": {
"type": "string"
},
"message": {
"type": "string",
"required": true
},
"quantity": {
"type": "number",
"required": true
},
"price": {
"type": "number",
"required": true
},
"status": {
"type": "string",
"required": true,
"default": "open"
}
},
data as:
{
"_index": "XXXXXX",
"_type": "XXXXX",
"_id": "XXXXXXX",
"_version": 1,
"_score": 1,
"_source": {
"name": "aadil kirana",
"email": "aadil#gmail.com",
"message": "dfgfb dgfggf",
"quantity": 3434,
"price": 5454,
"status": "open",
"createdAt": "2017-12-19T14:53:41.727Z",
"updatedAt": "2017-12-19T14:53:41.727Z"
}
}
Status could be open, processing, close, reject and failure.
All I want is to get the data in the order where I can see all the open status data ordered by createdAt date,
then all the prcoessing status data ordered by createdAt dat
and so on....
I tried using loopback filters as:
filter = {
order: ['status ASC','createdAt DESC'],
};
but this gives me First all the close status data ordered by date, then all the open status data ordered by date and so on, that status ordered alphabetically.
Please help me to get the desired result.

You can add a new property to your data as statusOrder and define
1 -> open
2 -> close
...
and order by statusOrder instead of status when you are ordering status.

All I want is to get the data in the order where I can see all the
open status data ordered by createdAt date, then all the prcoessing
status data ordered by createdAt dat and so on....
A workaround for this could be to let Elasticsearch do the sort with custom order e.g. in this context the status could be ordered as open followed by processing followed by close followed by reject followed by failure. It can be done with Function Score Query. Some more insights could also be found here
Sample input data for bulk insert:
POST custom/sort/_bulk?pretty
{"index" : {"_index" : "custom"}}
{"status": "open", "createdAt": "2017-12-19T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "open", "createdAt": "2017-12-18T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "processing", "createdAt": "2017-12-19T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "processing", "createdAt": "2017-12-17T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "close", "createdAt": "2017-12-19T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "close", "createdAt": "2017-12-19T15:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "failure", "createdAt": "2017-12-19T10:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "failure", "createdAt": "2017-12-19T14:59:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "reject", "createdAt": "2017-12-19T14:53:40.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "reject", "createdAt": "2017-12-19T14:53:41.727Z"}
Sample response from elastic search (without custom order):
Query:
GET custom/sort/_search?filter_path=took,hits.total,hits.hits._score,hits.hits._source
{
"took": 0,
"hits": {
"total": 10,
"hits": [
{
"_score": 1,
"_source": {
"status": "processing",
"createdAt": "2017-12-19T14:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "close",
"createdAt": "2017-12-19T14:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "reject",
"createdAt": "2017-12-19T14:53:40.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "open",
"createdAt": "2017-12-18T14:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "failure",
"createdAt": "2017-12-19T10:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "failure",
"createdAt": "2017-12-19T14:59:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "reject",
"createdAt": "2017-12-19T14:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "open",
"createdAt": "2017-12-19T14:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "processing",
"createdAt": "2017-12-17T14:53:41.727Z"
}
},
{
"_score": 1,
"_source": {
"status": "close",
"createdAt": "2017-12-19T15:53:41.727Z"
}
}
]
}
}
Query to mimic custom ordering :
GET custom/sort/_search?filter_path=took,hits.hits._id,hits.hits._score,hits.hits._source,hits.hits.sort
{
"query": {
"function_score": {
"boost_mode": "replace",
"query": {
"constant_score": {
"filter": {
"terms": {
"status.keyword": [
"open",
"processing",
"close",
"reject",
"failure"
]
}
}
}
},
"functions": [
{
"filter": {
"term": {
"status.keyword": "open"
}
},
"weight": 4
},
{
"filter": {
"term": {
"status.keyword": "processing"
}
},
"weight": 3
},
{
"filter": {
"term": {
"status.keyword": "close"
}
},
"weight": 2
},
{
"filter": {
"term": {
"status.keyword": "reject"
}
},
"weight": 1
},
{
"filter": {
"term": {
"status.keyword": "failure"
}
},
"weight": 0
}
]
}
},
"sort": [
{
"_score": {
"order": "desc"
},
"createdAt": {
"order": "asc"
}
}
]
}
Output (with custom order):
{
"took": 4,
"hits": {
"hits": [
{
"_id": "grOucmABwtSchlgLKlaV",
"_score": 4,
"_source": {
"status": "open",
"createdAt": "2017-12-18T14:53:41.727Z"
},
"sort": [
4,
1513608821727
]
},
{
"_id": "gbOucmABwtSchlgLKlaV",
"_score": 4,
"_source": {
"status": "open",
"createdAt": "2017-12-19T14:53:41.727Z"
},
"sort": [
4,
1513695221727
]
},
{
"_id": "hLOucmABwtSchlgLKlaV",
"_score": 3,
"_source": {
"status": "processing",
"createdAt": "2017-12-17T14:53:41.727Z"
},
"sort": [
3,
1513522421727
]
},
{
"_id": "g7OucmABwtSchlgLKlaV",
"_score": 3,
"_source": {
"status": "processing",
"createdAt": "2017-12-19T14:53:41.727Z"
},
"sort": [
3,
1513695221727
]
},
{
"_id": "hbOucmABwtSchlgLKlaV",
"_score": 2,
"_source": {
"status": "close",
"createdAt": "2017-12-19T14:53:41.727Z"
},
"sort": [
2,
1513695221727
]
},
{
"_id": "hrOucmABwtSchlgLKlaV",
"_score": 2,
"_source": {
"status": "close",
"createdAt": "2017-12-19T15:53:41.727Z"
},
"sort": [
2,
1513698821727
]
},
{
"_id": "ibOucmABwtSchlgLKlaV",
"_score": 1,
"_source": {
"status": "reject",
"createdAt": "2017-12-19T14:53:40.727Z"
},
"sort": [
1,
1513695220727
]
},
{
"_id": "irOucmABwtSchlgLKlaV",
"_score": 1,
"_source": {
"status": "reject",
"createdAt": "2017-12-19T14:53:41.727Z"
},
"sort": [
1,
1513695221727
]
},
{
"_id": "h7OucmABwtSchlgLKlaV",
"_score": 0,
"_source": {
"status": "failure",
"createdAt": "2017-12-19T10:53:41.727Z"
},
"sort": [
0,
1513680821727
]
},
{
"_id": "iLOucmABwtSchlgLKlaV",
"_score": 0,
"_source": {
"status": "failure",
"createdAt": "2017-12-19T14:59:41.727Z"
},
"sort": [
0,
1513695581727
]
}
]
}
}

Related

Elasticsearch query: combine nested array of objects into one array

Using Elasticsearch I am trying to combine a nested array of objects into one array.
This is what my data looks like:
GET invoices/_search
{
"hits": [
{
"_index": "invoices",
"_id": "1234",
"_score": 1.0,
"_source": {
"id": 1234,
"status": "unpaid",
"total": 15.35,
"payments": [
{
"id": 1981,
"amount": 10,
"date": "2022-02-09T13:00:00+01:00"
},
{
"id": 1982,
"amount": 5.35,
"date": "2022-02-09T13:35:00+01:00"
}
]
}
},
# ... More hits
]
}
I want to only get the payments array of each hit combined into one array, so that it returns something like this:
{
"payments": [
{
"id": 1981,
"amount": 10,
"date": "2022-02-09T13:00:00+01:00"
},
{
"id": 1982,
"amount": 5.35,
"date": "2022-02-09T13:35:00+01:00"
},
{
"id": 5658,
"amount": 3,
"date": "2021-12-19T13:00:00+01:00"
}
]
}
I tried to get this result using nested queries but could not figure it out, the query I used is:
# Query I used:
GET invoices/_search
{
"_source": ["payments"],
"query": {
"nested": {
"path": "payments",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "payments.id"
}
}
]
}
}
}
}
}
# Result:
{
"hits": [
{
"_index": "invoices",
"_id": "545960",
"_score": 1.0,
"_source": {
"payments": [
{
"date": "2022-01-22T15:38:15+01:00",
"amount": 374.5,
"id": 320320
},
{
"date": "2022-01-22T15:30:03+01:00",
"amount": 160.5,
"id": 320316
}
]
}
},
{
"_index": "invoices",
"_id": "545961",
"_score": 1.0,
"_source": {
"payments": [
{
"date": "2022-01-22T15:38:15+01:00",
"amount": 12,
"id": 320350
},
{
"date": "2022-01-22T15:30:03+01:00",
"amount": 60.65,
"id": 320379
}
]
}
}
]
}
The result returns only the payments array but divided over multiple hits. How can I combine those arrays?

Remove parent array and object elements from json - Strapi

Currently building an API with Strapi with the model of a blog post such that each Post has a Title, Slug, Content, and user Relation.
What data looks like:
{
"data": [
{
"id": 1,
"attributes": {
"title": "Test1",
"createdAt": "2022-07-24T18:33:34.195Z",
"updatedAt": "2022-07-24T18:33:34.863Z",
"publishedAt": "2022-07-24T18:33:34.861Z",
"user": {
"data": {
"id": 1,
"attributes": {
"username": "xyz",
"email": "xyz#gmail.com",
"provider": "local",
"confirmed": false,
"blocked": false,
"createdAt": "2022-07-24T14:28:16.466Z",
"updatedAt": "2022-07-24T14:29:00.126Z"
}
}
}
}
}
],
}
What I want it to look like:
{
{
"id": 1,
"title": "Test1",
"createdAt": "2022-07-24T18:33:34.195Z",
"updatedAt": "2022-07-24T18:33:34.863Z",
"publishedAt": "2022-07-24T18:33:34.861Z",
"user": {
"id": 1,
"username": "xyz",
"email": "xyz#gmail.com",
"provider": "local",
"confirmed": false,
"blocked": false,
"createdAt": "2022-07-24T14:28:16.466Z",
"updatedAt": "2022-07-24T14:29:00.126Z"
}
}
}
By default, the data is wrapped in unnecessarily tiresome arrays and objects and any attempt to edit the scehma.json causes the API to crash.
How can I fix this?
you can try this solution :
const x = {
"data": [
{
"id": 1,
"attributes": {
"title": "Test1",
"createdAt": "2022-07-24T18:33:34.195Z",
"updatedAt": "2022-07-24T18:33:34.863Z",
"publishedAt": "2022-07-24T18:33:34.861Z",
"user": {
"data": {
"id": 1,
"attributes": {
"username": "xyz",
"email": "xyz#gmail.com",
"provider": "local",
"confirmed": false,
"blocked": false,
"createdAt": "2022-07-24T14:28:16.466Z",
"updatedAt": "2022-07-24T14:29:00.126Z"
}
}
}
}
}
],
}
const data = x.data[0]
const users = {id: data.attributes.user.data.id , ...data.attributes.user.data.attributes}
const result = {id: data.id , title: data.attributes.title , createdAt: data.attributes.createdAt , updatedAt: data.attributes.updatedAt , publishedAt: data.attributes.publishedAt , user: users }
```

Filter Out duplicate arrays and return the unique array in mongodb aggregation

I have come a long way in structuring into the following mongodb data collection, but i couldn't finish the aggregation stage,
{
"test": [
{
"_id": "60014aee808bc5033b45c222",
"name": "a rogram",
"companyName": "company NAme",
"website": "https://www.example.comn",
"loginUrl": "https://www.example.comn",
"description": null,
"createdBy": "5fe5cbcdb9ac0f001dccfadf",
"createdAt": "2021-01-15T07:57:34.499Z",
"updatedAt": "2021-01-15T13:09:09.417Z",
"__v": 0,
"address": null,
"affiliatePlatform": "asdf",
"brands": 3,
"newList": [
{
"_id": "5feee63fd86250046d64d02e",
"name": "NEWBRAND",
"affiliateProgram": "60014aee808bc5033b45c222",
"operator": "scdscacscasc",
"description": null,
"established": "2021-01-20T12:39:25.000Z",
"createdAt": "2021-01-01T09:07:11.180Z",
"updatedAt": "2021-01-15T12:39:36.898Z",
"__v": 0,
"updatedBy": null,
"newPMBList": [
{
"_id": "5feee78ead36a5052cdaddc0",
"projectMarket": "5feb10c4906c880076ce5fa6",
"brand": "5feee63fd86250046d64d02e",
"createdAt": "2021-01-01T09:12:46.588Z",
"updatedAt": "2021-01-01T09:12:46.588Z",
"__v": 0,
"newPMList": [
{
"_id": "5feb10c4906c880076ce5fa6",
"project": "5feb10b6906c880076ce5fa5",
"market": "5feb0f44906c880076ce5f9d",
"url": "https://www.example.com/place",
"createdAt": "2020-12-29T11:19:32.945Z",
"updatedAt": "2020-12-29T11:19:32.945Z",
"__v": 0
}
]
}
]
},
{
"_id": "5ff07fce63da300174a014bb",
"name": "Nike",
"operator": "scdscacscasc",
"createdAt": "2021-01-02T14:14:38.607Z",
"updatedAt": "2021-01-15T08:05:09.475Z",
"__v": 0,
"description": "This is new afdkjnvlaf",
"established": "2021-01-27T08:02:38.000Z",
"updatedBy": null,
"affiliateProgram": "60014aee808bc5033b45c222",
"newPMBList": [
{
"_id": "5ff451891136a7006bc2b0eb",
"projectMarket": "5feb10c4906c880076ce5fa6",
"brand": "5ff07fce63da300174a014bb",
"createdAt": "2021-01-05T11:46:17.745Z",
"updatedAt": "2021-01-05T11:46:17.745Z",
"__v": 0,
"newPMList": [
{
"_id": "5feb10c4906c880076ce5fa6",
"project": "5feb10b6906c880076ce5fa5",
"market": "5feb0f44906c880076ce5f9d",
"url": "https://www.example.com/place",
"createdAt": "2020-12-29T11:19:32.945Z",
"updatedAt": "2020-12-29T11:19:32.945Z",
"__v": 0
}
]
}
]
},
{
"_id": "5fff0336d78339005812aaa4",
"name": "United Bank",
"affiliateProgram": "60014aee808bc5033b45c222",
"operator": "scdsc",
"description": "ascasccsac",
"established": null,
"createdAt": "2021-01-13T14:27:02.931Z",
"updatedAt": "2021-01-15T12:43:34.857Z",
"__v": 0,
"updatedBy": null,
"newPMBList": []
}
],
"projectsNumber": null
},
{
"_id": "6001923a1f1fb007437dc479",
"name": "Hotel Advertisement",
"companyName": "Atlas",
"website": "https://www.atlas.com",
"loginUrl": "https://www.atlas.com/luser/ogin",
"createdBy": "5fe5cbcdb9ac0f001dccfadf",
"createdAt": "2021-01-15T13:01:46.715Z",
"updatedAt": "2021-01-15T13:20:42.757Z",
"__v": 0,
"address": null,
"affiliatePlatform": "NetRefer",
"description": null,
"brands": 2,
"newList": [
{
"_id": "5ffff156f06d4700e255cde5",
"name": "Amazon",
"affiliateProgram": "6001923a1f1fb007437dc479",
"operator": "scdscacscasc",
"description": "vsdfvsfdv",
"established": null,
"createdAt": "2021-01-14T07:23:02.455Z",
"updatedAt": "2021-01-15T13:19:59.522Z",
"__v": 0,
"updatedBy": null,
"newPMBList": []
},
{
"_id": "60000d4c61316a01d2fbb1aa",
"name": "Toshiba",
"affiliateProgram": "6001923a1f1fb007437dc479",
"operator": "scdsc",
"description": "cvadfvfdvbfdav",
"established": null,
"createdAt": "2021-01-14T09:22:20.306Z",
"updatedAt": "2021-01-15T13:20:10.970Z",
"__v": 0,
"updatedBy": null,
"newPMBList": []
}
],
"projectsNumber": null
}
]
}
in the document above i would like to only return the distinct list of the newPMList along with the other data except the newList array like so:
"_id": "60014aee808bc5033b45c222",
"name": "21 Program",
"companyName": "Online Casino",
"website": "https://www.online.casinoe",
"loginUrl": "https://www.online.casino",
"description": null,
"createdBy": "5fe5cbcdb9ac0f001dccfadf",
"createdAt": "2021-01-15T07:57:34.499Z",
"updatedAt": "2021-01-15T13:09:09.417Z",
"__v": 0,
"address": null,
"affiliatePlatform": "Cellxpert",
"brands": 3,
"newPMList": [
{
"_id": "5feb10c4906c880076ce5fa6",
"project": "5feb10b6906c880076ce5fa5",
"market": "5feb0f44906c880076ce5f9d",
"url": "https://www.addissoftware.com/ethiopia",
"createdAt": "2020-12-29T11:19:32.945Z",
"updatedAt": "2020-12-29T11:19:32.945Z",
"__v": 0
}
]
per a document, but i couldn't any help is appreciated.
db.collection.aggregate([
{//Denormalize first level
"$unwind": "$newList"
},
{//Second nested level
"$unwind": "$newList.newPMBList"
},
{//Deep nested last level
"$unwind": "$newList.newPMBList.newPMList"
},
{
$group: {//Grouping back
"_id": null,
"newList": {
$push: "$newList.newPMBList.newPMList"
}
}
},
{
$project: {//Finding unique
newList: {
$setUnion: [
"$newList",
"$newList"
]
}
}
}
])
I suggest you to include other fields using first accumulator in group and preserve them in project.
You can simplify further as below
db.test.aggregate([
{
"$unwind": "$newList"
},
{
"$unwind": "$newList.newPMBList"
},
{
"$unwind": "$newList.newPMBList.newPMList"
},
{
$group: {
"_id": null,
"newList": {//addToSet keeps distinct
$addToSet: "$newList.newPMBList.newPMList"
}
}
}
])
Further, buy skipping one denormalisation, But it returns array of arrays.
db.test.aggregate([
{
"$unwind": "$newList"
},
{
"$unwind": "$newList.newPMBList"
},
{
$group: {
"_id": null,
"newList": {
$addToSet: "$newList.newPMBList.newPMList"
}
}
}
])
If you skip another level, it will add one more nested level in the result.

How to sort words by their relevance to a keyword?

I'm currently working on a search using elasticsearch. We have a very large amount of users.
Here is the elasticsearch mapping:
PUT /example_index/_mapping/users
{
"properties": {
"user_autocomplete": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"completion": {
"type": "text",
"analyzer": "user_autocomplete_analyzer",
"search_analyzer": "standard"
}
}
},
"firstName": {
"type": "text"
},
"lastName": {
"type": "text"
}
}
}
Here is the search query.
For example, I get 3 records
GET example_index/users/_search
{
"from": 0,
"size": 3,
"query": {
"query_string": {
"query": "*ro*",
"fields": [
"firstName",
"lastName"
]
}
},
"aggs": {
"user_suggestions": {
"terms": {
"size": 3,
"field": "user_autocomplete.raw"
}
}
}
}
Here is the output of elasticsearch
{
"took": 53,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 13,
"max_score": 1,
"hits": [
{
"_index": "example_index",
"_type": "users",
"_id": "08",
"_score": 1,
"_source": {
"firstName": "Eero",
"lastName": "Saarinen",
"user_autocomplete": "Eero Saarinen"
}
},
{
"_index": "example_index",
"_type": "users",
"_id": "16",
"_score": 1,
"_source": {
"firstName": "Aaron",
"lastName": "Judge",
"user_autocomplete": "Aaron Judge"
}
},
{
"_index": "example_index",
"_type": "users",
"_id": "20",
"_score": 1,
"_source": {
"firstName": "Robert",
"lastName": "Langdon",
"user_autocomplete": "Robert Langdon"
}
}
]
},
"aggregations": {
"user_suggestions": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 10,
"buckets": [
{
"key": "Eero Saarinen",
"doc_count": 1
},
{
"key": "Aaron Judge",
"doc_count": 1
},
{
"key": "Robert Langdon",
"doc_count": 1
}
]
}
}
}
I need result like in the following order:
Robert Langdon
Aaron Judge
Eero Saarinen
I have tried order method. It won't work. Is there a way to do this?

elasticsearch ngrams results are wrong

I am trying to implement partial matching using ngrams in elasticsearch but not getting the expected results out of it.
I am following this link:-
https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html
I have done all the things which are mentioned in this link. My dataset contains 3 fields i.e id,name,age.
Here is my mapping and setting of my_index
GET /my_index/_settings
{
"my_index": {
"settings": {
"index": {
"creation_date": "1433249154544",
"uuid": "hKxHVnqaRVmji31xK92pVA",
"number_of_replicas": "1",
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": "1",
"max_gram": "20"
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"filter": [
"lowercase",
"autocomplete_filter"
],
"tokenizer": "standard"
}
}
},
"number_of_shards": "1",
"version": {
"created": "1040499"
}
}
}
}
}
GET /my_index/_mapping/my_type
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"#timestamp": {
"type": "date",
"format": "dateOptionalTime"
},
"#version": {
"type": "string"
},
"age": {
"type": "long"
},
"host": {
"type": "string"
},
"id": {
"type": "string"
},
"message": {
"type": "string"
},
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
},
"path": {
"type": "string"
},
"type": {
"type": "string"
}
}
}
}
}
}
My query to elastic search is :-
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": {
"query": "raman r"
}
}
}
}
According to me now the results should display only "raman ram" but it is also showing other results as well:-
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 2.6631343,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 2.6631343,
"_source": {
"message": [
"2,raman,23"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "2",
"name": "raman",
"age": 23
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "10",
"_score": 1.8003473,
"_source": {
"message": [
"10,raman ram,43"
],
"#version": "1",
"#timestamp": "2015-06-02T13:11:03.455Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "10",
"name": "raman ram",
"age": 43
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_score": 0.26245093,
"_source": {
"message": [
"1,Ram,342"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.040Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "1",
"name": "Ram",
"age": 342
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "3",
"_score": 0.26245093,
"_source": {
"message": [
"3,ramayan,23"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "3",
"name": "ramayan",
"age": 23
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "4",
"_score": 0.26245093,
"_source": {
"message": [
"4,ramaram,231"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "4",
"name": "ramaram",
"age": 231
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "5",
"_score": 0.26245093,
"_source": {
"message": [
"5,rampy,1"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "5",
"name": "rampy",
"age": 1
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "6",
"_score": 0.26245093,
"_source": {
"message": [
"6,ration,11"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "6",
"name": "ration",
"age": 11
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "7",
"_score": 0.26245093,
"_source": {
"message": [
"7,rita,42"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.042Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "7",
"name": "rita",
"age": 42
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "8",
"_score": 0.26245093,
"_source": {
"message": [
"8,roni,45"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.050Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "8",
"name": "roni",
"age": 45
}
}
]
}
}
I get correct results when i use "minimum_should_match": "100%" while querying.
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": {
"query": "raman r",
"minimum_should_match": "100%"
}
}
}
}
Gave me better result although ranking is not right:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.6631343,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 2.6631343,
"_source": {
"message": [
"2,raman,23"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "2",
"name": "raman",
"age": 23
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "10",
"_score": 1.8003473,
"_source": {
"message": [
"10,raman ram,43"
],
"#version": "1",
"#timestamp": "2015-06-02T13:11:03.455Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "10",
"name": "raman ram",
"age": 43
}
}
]
}
}
Don't know whether this approach is correct or not but do tell me if any alternative is there for this

Resources