# Issue on db query

**URL:** https://discuss.elastic.co/t/issue-on-db-query/343369
**Category:** Kibana
**Created:** [September 19, 2023, 1:27pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369 "2023-09-19T13:27:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![girolamo](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@girolamo](https://discuss.elastic.co/u/girolamo)
#### Post date: [September 19, 2023, 1:27pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/1 "2023-09-19T13:27:42Z")

</div>

Hello there,  
I'm having issues making a query on a Elasticsearch db.

Indeed, if I make this request:

```auto
POST my-index/_search

```

I get almost all documents in the index.  
In this way:

````auto
{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1016,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index",
        "_type" : "_doc",
        "_id" : "c1d69d7c-8ec6-4623-b550-1c0570882810",
        "_score" : 1.0,
        "_source" : {
          "document_id" : "c1d69d7c-8ec6-4623-b550-1c0570882810",
          "document_name" : "1234_MYDOC.pdf",
          "document_project" : [
            "A PROJECT NAME"
          ],
          "document_path" : "c1d69d7c-8ec6-4623-b550-1c0570882810/1234_MYDOC.pdf",
          "uploaded_at" : "2023-04-14T07:38:25.672572",
          "uploaded_by" : "user@mail.com",
          "status" : "FAILED",
          "document_text" : null,
          "status_message" : "some error"
        }
      }
      ... #more documents here

      ]```

The issue I'm facing is while trying to get a result by document id.
Such as

````

```auto
POST /my-index/_search
{
  "query": {
    "term": {
      "document_name": "1234_MYDOC.pdf"
    }
  }
}

```

A query like this, will return

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "[document_name] query malformed, no start_object after query name",
        "line" : 2,
        "col" : 30
      }
    ],
    "type" : "parsing_exception",
    "reason" : "[document_name] query malformed, no start_object after query name",
    "line" : 2,
    "col" : 30
  },
  "status" : 400
}

```

Similar 400 results also happen when I search for a catchall term.  
Could you help?

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 19, 2023, 2:01pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/2 "2023-09-19T14:01:34Z")

</div>

Hi @girolamo

Syntax error see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html#term-query-ex-request)

```auto
GET /my-index/_search
{
  "query": {
    "term": {
      "document_name": {
        "value": "1234_MYDOC.pdf"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![girolamo](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@girolamo](https://discuss.elastic.co/u/girolamo)
#### Post date: [September 19, 2023, 2:13pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/3 "2023-09-19T14:13:13Z")

</div>

Hi Stephen, the solution you proposed isn't working,  
but this is:

```auto
POST my-index/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {"document_name": "1234_MYDOC.pdf"}}
      ]
    }
  }
}

```

The reason is unknown, yet the term-level query documentation is really useful. Thank you

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 19, 2023, 2:20pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/4 "2023-09-19T14:20:23Z")

</div>

What is your mapping... That is a `match` (full text) not a `term` (exact) query'

What are you trying to accomplish?

Please share the results of this which will show the mapping I suspect you did not make a mapping.

`GET my-index`

If you did not make a mapping then the query will be

```auto
GET /my-index/_search
{
  "query": {
    "term": {
      "document_name.keyword": {
        "value": "1234_MYDOC.pdf"
      }
    }
  }
}

```

You should learn about mappings (schema) and what you will get if you do not create a mapping ... i.e. the default mapping which for strings will create a multi-field with both `text` and `keyword` types

---

<div class="post-metadata">

### Author: ![girolamo](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@girolamo](https://discuss.elastic.co/u/girolamo)
#### Post date: [September 20, 2023, 11:10am UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/5 "2023-09-20T11:10:37Z")

</div>

Hello Stephen,  
here is what I get when I perform `GET my-index`

```auto
{
  "my-index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "document_bu" : {
          "type" : "text"
        },
        "document_id" : {
          "type" : "text"
        },
        "document_name" : {
          "type" : "text",
          "fields" : {
            "completion" : {
              "type" : "completion",
              "analyzer" : "simple",
              "preserve_separators" : true,
              "preserve_position_increments" : true,
              "max_input_length" : 100
            },
            "kw" : {
              "type" : "keyword"
            },
            "wc" : {
              "type" : "wildcard"
            }
          }
        },
        "document_path" : {
          "type" : "text"
        },
        "document_text" : {
          "type" : "text"
        },
        "status" : {
          "type" : "text"
        },
        "status_message" : {
          "type" : "text"
        },
        "uploaded_at" : {
          "type" : "date"
        },
        "uploaded_by" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "3",
        "provided_name" : "my-index",
        "creation_date" : "1634544853406",
        "number_of_replicas" : "1",
        "uuid" : "I1QsG-hHSauHzpFUfgcXzQ",
        "version" : {
          "created" : "7100199",
          "upgraded" : "7100299"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 20, 2023, 1:49pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/6 "2023-09-20T13:49:57Z")

</div>

So that is an interesting mapping did you create that yourself?

> [@girolamo](#):
>
> ```auto
> "document_name" : {
> "type" : "text",
> "fields" : {
> "completion" : {
> "type" : "completion",
> "analyzer" : "simple",
> "preserve_separators" : true,
> "preserve_position_increments" : true,
> "max_input_length" : 100
> },
> "kw" : {
> "type" : "keyword" <!----- keyword type subfield
> },
> "wc" : {
> "type" : "wildcard"
> }
> }
> }
> 
> ```

So if you want to do an term query then you would use the `keyword` subfield

```auto
GET /my-index/_search
{
  "query": {
    "term": {
      "document_name.kw": { 
        "value": "1234_MYDOC.pdf"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 18, 2023, 1:50pm UTC](https://discuss.elastic.co/t/issue-on-db-query/343369/7 "2023-10-18T13:50:21Z")

</div>

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