How can I do a one-to-one search in the title field?

Hello friends, I have a problem, I would be very happy if you could help. I want it to return exactly the string I want in a query using Wilcard, but it doesn't work. I have a document and document's title:

"Title" : "BREMAX SA"

The document id of this document is:

"_id" : "rMEMC3oBChFNLIc8INEm"

When I search using match with id, the result is returned successfully, but when I search with title field, no results are returned. That's why I'm using wildcard, but still no results.

when i search using document id:

GET my_index_name/_search
{
  "query": {
    "match": {
      "_id": "rMEMC3oBChFNLIc8INEm"
    }
  }
}

result returns successfully:

"hits" : [
      {
        "_index" : "my_index_name",
        "_type" : "_doc",
        "_id" : "rMEMC3oBChFNLIc8INEm",
        "_score" : 1.0,
        "_source" : {
          "CreateTime" : "2021-06-14T18:03:01.9387582+03:00",
          "Title" : "BREMAX SA",
          "OriginCountryId" : 24972,
          "CreatedBy" : 1,
          "IsActivated" : false,
          "IsCancelled" : false,
          "IsMembershipTypeBought" : false
        }
      }
    ]

When I search with wildcard or match using the title field:

GET my_index_name/_search
{
  "query": {
    "wildcard": {
      "Title": {
        "value": "*BREMAX SA*",
        "case_insensitive": true
      }
    }
  }
}

no results found:
"hits" : [ ]
I would be very grateful if you could help :blush:

Hi there,

Elasticsearch uses tokenizers for fields, when you index "BREMAX SA" Elasticsearch uses standard analyzer which tokenize field content using non-letter and space.

analyzers

So when you index BREMAX SA the inverted index, where the full-text search happens, there are two separate tokens created, BREMAX and SA, because there are no "BREMAX SA" token you cannot wildcard match it using * before and after.

In your current state what you can do is search for "BREMAX" or "SA", which in its own is a very bad idea Elasticsearch specifically mentions not to start and end with wildcard symbols.

If you want to be able to search for "BREMAX SA" with wildcards you need keyword tokenizer in your analyzer, which will keep the string inside the field whole.

edit:

or you can use a multifield keyword like this;

GET test/_search
{
  "query": {
    "wildcard": {
      "Title.keyword": {
        "value": "*BREMAX SA*",
        "case_insensitive": true
      }
    }
  }
}

but this approach is not what you are looking for in my opinion.

Best regards.

1 Like

When I tried with .keyword it failed again, but when I tried with .raw it succeeded..

GET my_index_name/_search
{
  "query": {
   "wildcard": {
     "Title.raw": {
       "value": "*BREMAX SA*",
       "case_insensitive": true
     }
   }
  }
}

@can.ozdemir thank you so much for helping :hugs:

hello @can.ozdemir . I have a problem again. I have a field whose type is text under two fields whose type is nesten. I want to search using wildcard on this field. However, this is not possible and no results are returned.

"Field1" : {
    "type" : "nested",
    "properties" : {
        "Field2" : {
          "type" : "nested",
          "properties" : { 
               "Field3" : { 
                  "type" : "text" 
                } 
          }
        }
    }
}

When I run (.raw or .keyword) the following query, no results are found:

GET my_index_name/_search
{
  "query": {
    "nested": {
      "path": "Field1.Field2",
      "query": {
        "bool": {
          "must": [
            {"wildcard": {
              "Field1.Field2.Field3.raw": {
                "value": "*FYX-F*",
                "case_insensitive":true
              }
            }
            }
          ]
        }    
      }
    }
  }
}

What is your opinion I can do ?

Hi,

Field3 doesn't have a raw field type, it only has text;

"properties" : { 
               "Field3" : { 
                  "type" : "text" 
                } 
          }

You do not need to put .raw at the end there, you are using the text field.

I tried without putting it but it doesn't work.

Yes it is not going to work since "text" fields are analyzed by standard analyzer. I explained this on the first question you asked. "non-letters and spaces" standard analyzer will create two tokens again FYX and F so you need to change your mapping and add a keyword field to "Field3" or you should change your tokenizer to keyword to keep the field whole.

1 Like

Do I need to reindex to do this?

It depends on your current mappings, just try to update your current mapping and add a fields property to your Field3 use the link below;

https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html

when you are updating an index use the _mapping endpoint;

PUT your-index-name/_mapping

ps: this is getting derailed please publish your further questions about other topics on other questions.

Okay, thank you so much for all your help.

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