Issue on db query

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

Indeed, if I make this request:

POST my-index/_search

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

{
  "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
POST /my-index/_search
{
  "query": {
    "term": {
      "document_name": "1234_MYDOC.pdf"
    }
  }
}

A query like this, will return

{
  "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?

Hi @girolamo

Syntax error see here

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

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

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

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

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

1 Like

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

{
  "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"
        }
      }
    }
  }
}


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

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

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

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