Not getting correct results when using special characters in query string

(shyam kishore mahato) #1

Hi am new to elastic search and trying to use query string as below but not getting the excat matches if use special characters

GET_search
{
    "query": { 
         "bool": { 
               "must": { 
                     "query_string": { 
                             "query":"*lr-*", 
                                      "fields": ["title","description"]
                        }
                   }
              }
     }
}
 
 
GET_search
{
     "query": { 
          "bool": { 
                 "must": { 
                       "query_string": { 
                               "query":"*lr/*", 
                                        "fields": ["title","description"]
                          }
                  }
            }
      }
}

Can anyone please help me out

Thanks in advance
Shyam

(David Pilato) #2

It depends on documents, mapping...

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.

(shyam kishore mahato) #3

Hi

I cannot recreate the index as i only acessing it please dind the mappings what i got

{
  "test-index": {
    "mappings": {
      "names": {
        "properties": {
          "Feild1": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "Feild2": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Regards
Shyam

(David Pilato) #4

You can always reproduce an example in a blank new test cluster.

Anyway, your problem is because of the way elasticsearch is analyzing and indexing the text.
Note that your example does not contain a title field or a description.

If you need more help, please provide a working example as I suggested.

(shyam kishore mahato) #5

Hi

I created a test_index and sharing the details

PUT test_index/1/101
{
  "title": "LR-123QZE",
  "description": "LR-123QZE description"
}
PUT test_index/1/102
{
  "title": "Lr-124QZE",
  "description": "Lr-124QZE description"
}
PUT test_index/1/103
{
  "title": "lr-125QZE",
  "description": "lr-125QZE description"
}
PUT test_index/1/104
{
  "title": "LR-123QZE",
  "description": "LR 126QZE description"
}
PUT test_index/1/105
{
  "title": "LR-123QZE",
  "description": "lr 123QZE description"
}
PUT test_index/1/106
{
  "title": "ID/123WDER",
  "description": "ID/123WDER description"
}


GET test_index/_search
{
  "query": {  
		"bool": {  
			"must": {  
				"query_string": {  
					"query":"*lr-*",                ///comment: "query":"*LR-*" or 	"query":"*lr\\-*",  or 	"query":"*LR\\-*", 
					"fields": ["title","description"]
				}
			}
		}
	}
}

result:

    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
      }

expected result:

    "hits": [
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "101",
            "_score": 1,
            "_source": {
              "title": "LR-123QZE",
              "description": "LR-123QZE description"
            }
          },
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "103",
            "_score": 1,
            "_source": {
              "title": "lr-125QZE",
              "description": "lr-125QZE description"
            }
          },
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "104",
            "_score": 1,
            "_source": {
              "title": "LR-123QZE",
              "description": "LR 126QZE description"
            }
          },
          {
            "_index": "test_index",
            "_type": "1",
            "_id": "102",
            "_score": 1,
            "_source": {
              "title": "Lr-124QZE",
              "description": "Lr-124QZE description"
            }
          }
        ]
      }

Query:

      GET test_index/_search
    {
      "query": {  
    		"bool": {  
    			"must": {  
    				"query_string": {  
    					"query":"*ID/*", 
    					"fields": ["title","description"]
    				}
    			}
    		}
    	}
    }

result: error

    {
      "error": {
        "root_cause": [
          {
            "type": "query_shard_exception",
            "reason": "Failed to parse query [*ID/*]",
            "index_uuid": "aXYtffzVTFeQsxtxEDyGgg",
            "index": "test_index"
          }
        ]

expected result:error

Query

      GET test_index/_search
    {
      "query": {  
    		"bool": {  
    			"must": {  
    				"query_string": {  
    					"query":"*ID\\/*", 
    					"fields": ["title","description"]
    				}
    			}
    		}
    	}
    }

result:

    "hits": {
        "total": 0,
        "max_score": null,
        "hits": [\*]
      }
     ``````
     expected result:
 "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
     {
        "_index": "test_index",
        "_type": "1",
        "_id": "106",
        "_score": 1,
        "_source": {
          "title": "ID/123WDER",
          "description": "ID/123WDER description"
        }
      }
    ]
  }
mapping details
  {
  "test_index": {
    "mappings": {
      "1": {
        "properties": {
          "description": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Regards,
Shyam
(David Pilato) #6

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Here search in title.keyword instead.

(shyam kishore mahato) #7

Hi

Sorry for the formatting issue. I have corrected it now.

I tried with title.keyword but now it returning only case sensitive results
I need all data irrespective of the case

GET test_index/_search
{
  "query": {  
		"bool": {  
			"must": {  
				"query_string": {  
					"query":"*lr-*", 
					"fields": ["title.keyword","description.keyword"]
				}
			}
		}
	}
}

result:

"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "1",
        "_id": "103",
        "_score": 1,
        "_source": {
          "title": "lr-125QZE",
          "description": "lr-125QZE description"
        }
      }
    ]
  }

Regards,
Shyam

(David Pilato) #8

That's expected. You can define a lowercase normalizer to the keyword field or change the analyzer of the title field to simple.

(shyam kishore mahato) #9

Hi,

For this I need to re-create my index? and if yes in all PUT calls I need to add the normalizer or analyzer. and can you please provide some sample examples for the above both ways.

Regards,
Shyam

(David Pilato) #10

For this I need to re-create my index?

Yes.

can you please provide some sample examples for the above both ways.

The documentation is normally pretty much explicit:

(shyam kishore mahato) #11

Hi,

Thanks for support. Will try to use normalizer or analyzer .

Regards,
Shyam

(shyam kishore mahato) #12

Hi ,

Can we achieve 'keyword'case sensitivity without analyzer or normalizer without changing the index?

Thanks ,
Shyam

(David Pilato) #13

Not really in a fast way at least.

(shyam kishore mahato) #14

Hi

I tried the changes it’s working but not for all special characters i need to append \ for each time. Is there any way to replace all special characters by handling in query itself

Regards
Shyam

(David Pilato) #15

I can't really know with a fully working example. Read About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

(system) closed #16

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