Matchquery is unable to sort based on query

Hello -

I am trying to execute a query which has match query (nodeid)+ sort(with date) + size(1). I am trying to get the out N curries the latest requried nodeid details. And this cscfcounter index will be getting updated for every 60sec.

curl -X GET "localhost:9200/cscfcounter/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {"match" : {"NodeId" : "swevmware-cscf02"}},
"sort":[{"date" : {"order" : "desc"}}],
"size" : 1
}'

But when i execute , i am getting the output with a different nodeid i.e "swevmware-cscf01". Even though i made it swevmware-cscf02.

curl -X GET "localhost:9200/cscfcounter/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {"match" : {"NodeId" : "swevmware-cscf02"}},
"sort":[{"date" : {"order" : "desc"}}],
"size" : 1
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 38558,
"max_score" : null,
"hits" : [
{
"_index" : "cscfcounter",
"_type" : "cscfcounter",
"_id" : "AWO18thxI4ct1UZkHTaN",
"_score" : null,
"_source" : {
"CpuAverageLoad" : 1,
"HaGroupId" : "100001",
"LbGroupId" : "",
"MemFree" : 100,
"MemUsed" : 0,
"NodeId" : "swevmware-cscf01",
"NodeType" : "cscf",
"State" : "online",
"Static_limit" : 0,
"date" : "2018-05-31T11:26:21.296747036Z"
},
"sort" : [
1527765981296
]
}
]
}
}

But if execute the same command with size 2, then i am getting the both nodeid i.e cscf01 & cscf02.

curl -X GET "localhost:9200/cscfcounter/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {"match" : {"NodeId" : "swevmware-cscf02"}},
"sort":[{"date" : {"order" : "desc"}}],
"size" : 2
}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 38562,
"max_score" : null,
"hits" : [
{
"_index" : "cscfcounter",
"_type" : "cscfcounter",
"_id" : "AWO1876pI4ct1UZkHTaU",
"_score" : null,
"_source" : {
"CpuAverageLoad" : 1,
"HaGroupId" : "100001",
"LbGroupId" : "",
"MemFree" : 100,
"MemUsed" : 0,
"NodeId" : "swevmware-cscf01",
"NodeType" : "cscf",
"State" : "online",
"Static_limit" : 0,
"date" : "2018-05-31T11:27:20.23261639Z"
},
"sort" : [
1527766040232
]
},
{
"_index" : "cscfcounter",
"_type" : "cscfcounter",
"_id" : "AWO187lJI4ct1UZkHTaT",
"_score" : null,
"_source" : {
"CpuAverageLoad" : 0,
"HaGroupId" : "100001",
"LbGroupId" : "",
"MemFree" : 100,
"MemUsed" : 0,
"NodeId" : "swevmware-cscf02",
"NodeType" : "cscf",
"State" : "online",
"Static_limit" : 0,
"date" : "2018-05-31T11:27:18.855816443Z"
},
"sort" : [
1527766038855
]
}
]
}
}

I doubt when we sort by date with size1 it is taking the latest updated doc in that index.

Probably NodeId is a text field and not a keyword field. Check the mapping or try on NodeId.keyword if you are using defaults

The mapping is as follows as

"NodeId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}

Can you explain what you meant by NodeId.keyword?

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

image

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Here in your mapping you can see that you have 2 fields:

  • NodeId which has a type text
  • NodeId.keyword which has a type keyword

So search with:

GET cscfcounter/_search
{
  "query": {"match" : {"NodeId.keyword" : "swevmware-cscf02"}},
  "sort":[{"date" : {"order" : "desc"}}],
  "size" : 2
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.