M220P: Ticket: Text and Subfield Search:

Ticket: Text and Subfield Search

I am getting following error on command prompt when I am validating “Text and Subfield Search”
I have added query = {“genres”: {"$in" : filters[“genres”]}}
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: text index required for $text query

Any suggestions please.

Hello @amitchinche. Your message was related with “Text” filter.

Can you share your filters in build_query_sort_project ?

All the best,

JNetto

Hi Thanks for help,

I am running validation test for “Text and Subfield Search”.
I have not changed anything other than a query for genres.

======
def build_query_sort_project(filters):
“”"
Builds the query predicate, sort and projection attributes for a given
filters dictionary.
“”"
query = {}
# The field “tomatoes.viewer.numReviews” only exists in the movies we want
# to display on the front page of MFlix, because they are famous or
# aesthetically pleasing. When we sort on it, the movies containing this
# field will be displayed at the top of the page.
sort = [(“tomatoes.viewer.numReviews”, DESCENDING), ("_id", ASCENDING)]
project = None
if filters:
if “text” in filters:
query = {"$text": {"$search": filters[“text”]}}
meta_score = {"$meta": “textScore”}
sort = [(“score”, meta_score)]
project = {“score”: meta_score}
elif “cast” in filters:
query = {“cast”: {"$in": filters[“cast”]}}
elif “genres” in filters:

        """
        Ticket: Text and Subfield Search

        Given a genre in the "filters" object, construct a query that
        searches MongoDB for movies with that genre.
        """

        # TODO: Text and Subfield Search
        # Construct a query that will search for the chosen genre.
        query = {"genres": {"$in" : filters["genres"]}}

return query, sort, project

================
Below is test_test_and_subfield_search.py

“”"
Test file for database methods written in db.py

All test methods must receive client as an argument,
otherwise the database variable won’t be configured correctly
“”"
import pytest
from mflix.db import get_movies

@pytest.mark.text_and_subfield_search
def test_text_search(client):
# ensure you sort text results based on their metaScore!!
filter = {‘text’: ‘mongo’}
(movies, results) = get_movies(filter, 0, 20)
movies = list(movies)
assert len(movies) == 6
assert results == 6
assert movies[0].get(‘title’) == “Flash Gordon”

@pytest.mark.text_and_subfield_search
def test_genre_search(client):
filter = {‘genres’: [‘Action’]}
(movies, results) = get_movies(filter, 0, 20)
assert len(list(movies)) == 20
assert results == 2539

@pytest.mark.text_and_subfield_search
def test_multiple_genre_search(client):
filter = {‘genres’: [‘Action’, ‘Adventure’]}
(movies, results) = get_movies(filter, 0, 25)
assert len(list(movies)) == 25
assert results == 3805

@pytest.mark.text_and_subfield_search
def test_cast_search(client):
filter = {‘cast’: [‘Elon Musk’]}
(movies, results) = get_movies(filter, 0, 20)
assert len(list(movies)) == 1
assert results == 1

@pytest.mark.text_and_subfield_search
def test_multiple_cast_search(client):
filter = {‘cast’: [‘Elon Musk’, ‘Robert Redford’, ‘Julia Roberts’]}
(movies, results) = get_movies(filter, 0, 33)
assert (len(list(movies))) == 33
assert results == 62

This text is showing different types of double quotes like copy/pasted ? Can you remove the code and try running the test again if it shows assertion errors or the same message?

Kanika

Hi Kanika,
Thanks, I have removed the code and getting same message.

        # TODO: Text and Subfield Search
        # Construct a query that will search for the chosen genre.
        query = {}
        #query = {"genres": {"$in" : filters["genres"]}}

-======
Error message is same. Sorry I think earlier I did not provide entire message but the last 2-3 line, Now I am provide entire text. Please see below and suggest.

Traceback (most recent call last):
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1997, in call
return self.wsgi_app(environ, start_response)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1985, in wsgi_app
response = self.handle_exception(e)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_compat.py”, line 33, in reraise
raise value
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1982, in wsgi_app
response = self.full_dispatch_request()
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_compat.py”, line 33, in reraise
raise value
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1612, in full_dispatch_request
rv = self.dispatch_request()
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1598, in dispatch_request
return self.view_functionsrule.endpoint
File “C:\Users\eamchin\Documents\Python Scripts\mflix-python\mflix\api\movies.py”, line 68, in api_search_movies
filters, page, DEFAULT_MOVIES_PER_PAGE)
File “C:\Users\eamchin\Documents\Python Scripts\mflix-python\mflix\db.py”, line 227, in get_movies
total_num_movies = db.movies.count_documents(query)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py”, line 1685, in count_documents
sock_info, slave_ok, cmd, collation, session)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py”, line 1587, in _aggregate_one_result
session=session)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py”, line 244, in _command
retryable_write=retryable_write)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\pool.py”, line 579, in command
unacknowledged=unacknowledged)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\network.py”, line 150, in command
parse_write_concern_error=parse_write_concern_error)
File “C:\Users\eamchin\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\helpers.py”, line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: text index required for $text query

Sorry one more observation, please. With my earlier code, i.e. query = {“genres”: {"$in" : filters[“genres”]}}, when I tested it searching it is working fine. When I put various genres on the web UI search field, it is giving the right results but only thing is it is not validating in the status page.

The search on web UI is not working for “text” search, rest other searches are working.

It was my mistake, I did not do mongorestore and hence there was missing index which was causing problem in text search.

1 Like