cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

How do I read query an index in lucene style once I have built it? #213

Closed ayushidalmia closed 8 years ago

ayushidalmia commented 8 years ago

I am doing the following:

from cloudant import cloudant
import json
from cloudant.result import Result,ResultByKey
import random
from cloudant.design_document import DesignDocument
from cloudant.query import Query

with open('credentials.json') as f:
    cred  = json.load(f)

with cloudant(str(cred['credentials']['username']),str(cred['credentials']['password']),url=str(cred['credentials']['url'])) as client:

    my_database = client['abof']

    '''
    #query CURD
    with my_database.custom_result(include_docs=True) as rslt:
        temp = rslt[1:10]
    '''
    #create design doc
    ddoc = DesignDocument(my_database,"_design/DDOC/_view/INDEX")

    #add search index
    '''
    search_index = ('function (doc) {\n  index("default", doc._id); '
                        'if (doc._id) {index("name", doc.filename, '
                        '{"store": true}); }\n}')   
    '''
    search_index = "function(doc){index(\"default\", doc._id);if (doc.filename){index(\"filename\", doc.filename, {\"store\": true});}}"
    ddoc.add_search_index("freesearch", search_index, analyzer=None)

    #check views and indices
    print ddoc.list_indexes()

    #save to remote db
    try:
        ddoc.save()
    except:
        pass

    #query search index
    query = Query(my_database, selector={'_id': {'$gt': 0}},use_index="freesearch")
    result = query.custom_result()
    with query.custom_result() as rslt:
        temp=rslt[1:10]

    for t in temp:
        print t

However, it gives the following error:

requests.exceptions.HTTPError: 400 Client Error: Bad Request no_usable_index No index matches the index specified with "use_index" for url: https:****/_find

I am using Python 2.7 and cloudant 2.1.0 Also, what will be a good help place for python cloudant?

rhyshort commented 8 years ago

The cluster your account is under could be under the load, so the index is not available on the other nodes yet, does the problem persist if you try again?

ayushidalmia commented 8 years ago

Yes. It does.

Can you review the method in which I am querying the index?

ricellis commented 8 years ago

I would recommend using the get_search_result

ayushidalmia commented 8 years ago

It still throws the same error. I did the following:

#query search index
    resp = my_database.get_search_result("ddoc", "freesearch",
                            query='*:*',
                            include_docs=True)
    for row in resp['rows']:
        print row

And get this error:

Traceback (most recent call last):
  File "search.py", line 55, in <module>
    include_docs=True)
  File "/Library/Python/2.7/site-packages/cloudant/database.py", line 1355, in get_search_result
    resp.raise_for_status()
  File "/Library/Python/2.7/site-packages/requests/models.py", line 862, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Object Not Found not_found missing for url: https://3f4e3c29-dc84-48ba-833f-4fe71ed45ada-bluemix:87bf0c3b2df8edaad3ba29da3451aa5d4230bbda70cccca89447a570c50ef3cc@3f4e3c29-dc84-48ba-833f-4fe71ed45ada-bluemix.cloudant.com/abof/_design/ddoc/_search/freesearch

Can you review if the index is created?

ricellis commented 8 years ago

Are you sure you are searching with the correct design document name? In the earlier snippet you created a design document with the name DDOC/_view/INDEX:

ddoc = DesignDocument(my_database,"_design/DDOC/_view/INDEX")

but you are trying to search using the design document with the name ddoc which if it doesn't exist would explain the 404.

ayushidalmia commented 8 years ago

Thanks. It worked.

rhyshort commented 8 years ago

Great, glad to hear its working for you.