Neil
Neil

Reputation: 3036

elasticsearch does not return expected returns

I'm complete new on elasticsearch. I tried search API but it's not returning what I expected

What I did

POST /test/_doc/1
{
  "name": "Hello World"
}

GET /test/_doc/1
Response:
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 5,
  "_seq_no" : 28,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Hello World"
  }
}

GET /test/_mapping
Response:
{
  "test" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "query" : {
          "properties" : {
            "term" : {
              "properties" : {
                "name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

GET /test/_search
{
  "query": {
    "term": {
      "name": "Hello"
    }
  }
}:
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

GET /test/_search
{
  "query": {
    "term": {
      "name": "Hello World"
    }
  }
}
Response:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

My elasticsearch version is 7.3.2

The last two search should return me document 1, is that correct? Why does it hit nothing?

Upvotes: 1

Views: 58

Answers (1)

Gibbs
Gibbs

Reputation: 22956

Problem is that you have term queries. Term queries are not analysed. Hence Hello didn't match the term hello in your index. Note the case difference.

Unlike full-text queries, term-level queries do not analyze search terms. Instead, term-level queries match the exact terms stored in a field.

Reference

Whereas match queries analyse the search term also.

{
  "query": {
    "match": {
      "name": "Hello"
    }
  }
}

You can use _analyze to check how your terms are indexed.

Upvotes: 3

Related Questions