Home > Back-end >  querying on a new field in open search isnt retrieving results
querying on a new field in open search isnt retrieving results

Time:12-03

am using opensearch 2.4 and I have an index with some fields while creating , later i started saving new field to the index , now when i query on the newly created field am not getting any results

ex : query 1 POST abc/_search

{
  "query": {
    "bool": {
        "must": [
            {
                "terms": {
                    "name": [
                        "john"
                    ]
                }
            }
        ]
    }
}   
}

above works fine because name fields exists since creation of index

query 2 : POST abc/_search

{
  "query": {
    "bool": {
        "must": [
            {
                "terms": {
                    "lastname": [
                        "William"
                    ]
                }
            }
        ]
    }
}   
}

above query doesnt work though i have some documents with lastname william

CodePudding user response:

When you index a new field without previously declaring it in the mapping, opensearch/elastic will generate text type and type keyword.

There are two ways for you to get results with the Term Query. First remember that Term query works with exact terms.

The first option is to use the keyword field.

          {
            "terms": {
                "lastname.keyword": [
                    "William"
                ]
            }
        }

The second option is to search in the text field, but remember that when indexing the default parser is applied, then the lowecase filter leaves the token like this: william.

In this case, the query should be:

          {
            "terms": {
                "lastname": [
                    "william"
                ]
            }
        }

CodePudding user response:

When you use "terms" there must be an exact match (including casing).

So make sure your document contains William and not william or Williams

If you want more tolerance you can explore the match query:

https://opensearch.org/docs/latest/opensearch/query-dsl/full-text/#match

  • Related