How to index in Atlas Search a string field that store in fact a number

Hi everyone.
I have a collection called books with the following structure:

_id: string,
title: string,
authors: Array<string>,
isbn: string,
...

isbn field is in fact a number, although is store as string, i.e: "9781732539419". I want to create a Atlas Search index in order to be able to search in title, authors and isbn fields at the same time. I have created this simple search index:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "title": {
        "type": "string"
      },
      "authors": {
        "type": "string"
      },
      "isbn": {
        "type": "string"
      }
    }
  }
}

I have done some tests using the search tester of Atlas. Testing with strings that match partially title and authors works fine. The problem is with isbn field. If I write the full string, for example: 9781732539419, it works fine, but if I write the isbn partially, for example, 17325394 no books are returned. Why is this happening? It is a normal behavior of the Atlas Search?

Sounds like you’re using a hammer to squash a flea :grin:

Why not just do a $regex search on the “isbn” field?

1 Like

I have re-checked the documentation. Apparently everything is working as supposed, the index doesn’t search partial substrings, only words, and the isbn string is a single word. The index is not the best option in my case, because I would like to search into the three fields both words and substrings, but is enough to continue my app for now. Atlas Search is huge, I will explore more options in the future. Thanks anyway.

2 Likes

Hi @Ruben_FS , it sounds like you’re interested in matching substring in Atlas Search. Have you seen this article on partial matching. Let me know if you take a look and if it’s what you’re looking for.

Thank you for your answer @amyjian . I have taken a look the documentation and I have found very useful information. I have created this search index:

{
  "mappings": {
    "fields": {
      "isbn": {
        "analyzer": "lucene.keyword",
        "type": "string"
      },
      "title": {
        "analyzer": "lucene.keyword",
        "type": "string"
      }
    }
  }
}

It looks that this was what I’m looking for. Doing this query I get the desired results, because I can search both in title and isbn fields.

[
  {
    $search: {
      'index': 'mainSearchIndex',
      'compound': {
        'should': [
          {
            'regex': {
              'path': 'isbn',
              'query': '(.*)848327(.*)'
            }
          },
          {
            'regex': {
              'path': 'title',
              'query': '(.*)848327(.*)'
            }
          }
        ]
      }
    }
  }
]

Any advice is welcome.