Query String Query exact word search

Hi Guys! I want to return result that only has the exact word searched without appended characters like if I search "toy", it SHOULD NOT return "toys" or "toyed".

Right now, when I do some searching, it is returning results that doesn't have the word searched.

here's how I use it;

{

"size": 10,

"query": {

        "query_string" : {

                 "query" : "lawyer:(John Doe)",

                 "fuzziness":0

          }

 },

  "collapse" : {

       "field" : "id"

  }
}

Any help would be appreciated. Thanks!

We can help you better if you can add the mapping of the index.

Hi @Opster_Community1, Thanks for your response,

I actually put:

 "query_string" : {

             "query" : "lawyer.atty_name:(John Doe)",

             "fuzziness":0

      } 

here's the example result set:

hits": [
{
    "_index": "my_index",
    "_type": "_doc",
    "_id": "Uz3iPm0BznItGBZve8Yp",
    "_score": 9.699111,
    "_source": {
        "applicant":"Jerry Smith",
        "@timestamp": "2019-09-17T11:01:00.857Z",
        "lawyer": [
            {
                "atty_name": "John Doe",
                "atty_country": "US",
                "atty_org": null
            }
        ],
        
        "examiner": [
            {
                "name": "Alex Smith",
                "role": "primary"
            }
        ],
        "id": 6692053,
        "reldoc": [
            {
                "related_doc": "06336679"
            },
            {
                "related_doc": "06692053"
            }
        ]
   }
]

Anyone? :frowning:

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Hi @dadoonet, Thanks for your response, I just want to know if it is possible to search using query_string and have an exact word searched as a result. In this case I am searching for a name.

Here's what I did:

POST records/_search

  {
     "size": 1,
     "query": {
	    "query_string" : {
		"query" : "lawyer.atty_name:(John Doe)",
		"fuzziness":0
	  }
   },
      "collapse" : {
	    "field" : "id" 
      }
   }

Returned Result:

hits": [
{
   "_index": "records",
   "_type": "_doc",
   "_id": "Uz3iPm0BznItGBZve8Yp",
   "_score": 9.699111,
   "_source": {
      "applicant":"Jerry Smith",
      "id": 6692053
      "@timestamp": "2019-09-17T11:01:00.857Z",
      "lawyer": [
          {
               "atty_name": "Matthew Doe",
               "atty_country": "US",
               "atty_org": null
          }
    ],
    
    "examiner": [
        {
            "name": "Alex Smith",
            "role": "primary"
        }
    ],
    "reldoc": [
        {
            "related_doc": "06336679"
        }
    ]
   }
]

I wanted to get all result with "atty_name": John doe like this but instead I am getting a bunch of different names.

"lawyer": [
              {
                   "atty_name": "John Doe",
                   "atty_country": "US",
                   "atty_org": null
              }
        ]

Please inform me if there is still something you don't understand on my question. Thank you very much.

Please add mapping of the index. To get the mapping you can use:

GET <index>/_mapping

Thanks @Opster_Community1, Sorry, here's the mapping

{
"records": {
    "mappings": {
        "properties": {
            "@timestamp": {
                "type": "date"
            },
            "@version": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "id": {
                "type": "long"
            },
            "applicant": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "examiner": {
                "properties": {
                    "exm_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "role": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "lawyer": {
                "properties": {
                    "atty_country": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "atty_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "atty_org": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "reldoc": {
                "properties": {
                    "related_doc": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "tags": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

}

For making an exact search I would suggest you to use term query instead of query _string as term query is meant for the same. This can be done as below:

GET records/_search
{
  "query": {
    "term": {
      "lawyer.atty_name.keyword": "John Doe"
    }
  }
}

Still if you want to perform the search using query_string then you have to wrap the search string (John Doe) in double quotes as below:

GET records/_search
{
  "query": {
    "query_string": {
      "query": "lawyer.atty_name.keyword:\"John Doe\"",
      "fuzziness": 0
    }
  }
}

Note that I'm using keyword sub field since it is of type keyword which index the string as is without applying any analyzers on it.

1 Like

GREAT!! Thank you very much! I will use query_string because I have a very complex search combinations :slight_smile: Thanks Again!

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