Home > Blockchain >  Elasticsearch returning wrong results upon query
Elasticsearch returning wrong results upon query

Time:07-12

I am new to ElasticSearch and was doing some experiments to learn but I figured out that _search query is returning wrong results. I inserted documents to index by using following code

PUT tryDB/_doc/2
{"personId":"2","minor":true,"money":15 }


PUT tryDB/_doc/3
{"personId":"3","minor":true,"money":20 }


PUT tryDB/_doc/4
{"personId":"4","minor":true,"money":25 }


PUT tryDB/_doc/5
{"personId":"5","minor":true,"money":30 }


PUT tryDB/_doc/6
{"personId":"6","minor":true,"money":35 }


PUT tryDB/_doc/7
{"personId":"7","minor":true,"money":40 }


PUT tryDB/_doc/8
{"personId":"8","minor":true,"money":45 }


PUT tryDB/_doc/9
{"personId":"9","minor":true,"money":55 }


PUT tryDB/_doc/10
{"personId":"10","minor":true,"money":60 }


PUT tryDB/_doc/11
{"personId":"11","minor":true,"money":65 }


PUT tryDB/_doc/12
{"personId":"12","minor":true,"money":70 }


PUT tryDB/_doc/13
{"personId":"2","minor":false,"money":80 }


PUT tryDB/_doc/14
{"personId":"2","minor":false,"money":90 }


PUT tryDB/_doc/15
{"personId":"2","minor":false,"money":100 }


PUT tryDB/_doc/16
{"personId":"2","minor":false,"money":10 }

After which I fired up a GET tryDB/_search query to list all the documents, which in turn returns

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 16,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "tryDB",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "personId" : "1",
          "minor" : true,
          "money" : 10
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "personId" : "2",
          "minor" : true,
          "money" : 15
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "personId" : "3",
          "minor" : true,
          "money" : 20
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "personId" : "4",
          "minor" : true,
          "money" : 25
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "personId" : "5",
          "minor" : true,
          "money" : 30
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "personId" : "6",
          "minor" : true,
          "money" : 35
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "personId" : "7",
          "minor" : true,
          "money" : 40
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "personId" : "8",
          "minor" : true,
          "money" : 45
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "personId" : "9",
          "minor" : true,
          "money" : 55
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "10",
        "_score" : 1.0,
        "_source" : {
          "personId" : "10",
          "minor" : true,
          "money" : 60
        }
      }
    ]
  }
}

Where are the rest 6 documents ?

Now I went ahead and fired up a range based query

GET tryDB/_search
{
  "query": {
  "range": {
      "money": {
        "lte":100
        
      }
    }
  }
}

Which in turn returned

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "tryDB",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "personId" : "1",
          "minor" : true,
          "money" : 10
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "15",
        "_score" : 1.0,
        "_source" : {
          "personId" : "2",
          "minor" : false,
          "money" : 100
        }
      },
      {
        "_index" : "tryDB",
        "_id" : "16",
        "_score" : 1.0,
        "_source" : {
          "personId" : "2",
          "minor" : false,
          "money" : 10
        }
      }
    ]
  }
}

Which is wrong clearly. Can anyone help me figure out what's going on here?

CodePudding user response:

Where are the rest 6 documents ?

When you do not determine the value of "size", by default elastic returns 10 documents.

Set size like this:

{
  "size": 20, 
  "query": {
  "match_all": {}
  }
}

CodePudding user response:

POST tryDB/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "money": {
              "lte": 100
            }
          }
        }
      ]
    }
  }
}

CodePudding user response:

@rabbitbr Thanks for the quick response! Hey I figured out the solution (posting here) Based on the result, Looks like Elastic Search index money as string.

I tried setting up an explicit mapping to make sure the money field indexed as number. https://opensearch.org/docs/1.3/opensearch/mappings/

This worked out.

  • Related