php solr client

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    php solr client

    hi
    i am trying to use pache solr with php i have bitnami apache solr and php-solr client installed , i am trying to run the example code
    [code=PHP]
    <?php
    require_once( 'Solr/Service.php' );

    //
    //
    // Try to connect to the named server, port, and url
    //
    $solr = new Apache_Solr_Ser vice( 'localhost', '8983', '/solr' );

    if ( ! $solr->ping() ) {
    echo 'Solr service not responding.';
    exit;
    }
    //
    //
    // Create two documents to represent two auto parts.
    // In practice, documents would likely be assembled from a
    // database query.
    //
    $parts = array(
    'spark_plug' => array(

    'name' => 'Spark plug',

    ),
    'windshield' => array(

    'name' => 'Windshield',

    )
    );

    $documents = array();

    foreach ( $parts as $item => $fields ) {
    $part = new Apache_Solr_Doc ument();

    foreach ( $fields as $key => $value ) {
    if ( is_array( $value ) ) {
    foreach ( $value as $datum ) {
    $part->setMultiValu e( $key, $datum );
    }
    }
    else {
    $part->$key = $value;
    }
    }

    $documents[] = $part;
    }

    //
    //
    // Load the documents into the index
    //
    try {
    $solr->addDocuments ( $documents );
    $solr->commit();
    $solr->optimize();
    }
    catch ( Exception $e ) {
    echo $e->getMessage() ;
    }


    //
    //
    // Run some queries. Provide the raw path, a starting offset
    // for result documents, and the maximum number of result
    // documents to return. You can also use a fourth parameter
    // to control how results are sorted and highlighted,
    // among other options.
    //
    $offset = 0;
    $limit = 10;

    $queries = array(
    'name: plug'
    );


    foreach ( $queries as $query ) {
    $response = $solr->search( $query, $offset, $limit );
    echo '<pre>';
    echo $response->getHttpStatus( );
    print_r( $response->getRawResponse () );
    echo $response->response->numFound;
    echo '</pre>';
    break;
    if ( $response->getHttpStatus( ) == 200 ) {
    // print_r( $response->getRawResponse () );

    if ( $response->response->numFound > 0 ) {
    echo "$query <br />";

    foreach ( $response->response->docs as $doc ) {
    echo "$doc->partno $doc->name <br />";
    }

    echo '<br />';
    }
    }
    else {
    echo $response->getHttpStatusM essage();
    }
    }
    ?>
    [/code]

    but it is continiously giving 400 bad request error , can someone help me in this regard , i googled and most of the issues regarding 400 bad request were suggesting to define the fields in the schema.xml but one thing i cant understand is that for the very first time is there any need to load data into it which can be searched , I am confused there are 3 diferent things apache solr and php solr client i downloaded the phpsolr client from
    http://code.google.com/p/solr-php-client/downloads/list
    regards,
    Omer Aslam
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    i have solved it , this was on the commit() function because solr 4.0 and up versions have deprecated the waitflush parameter in the xml tag , we need to remove this from the optimize() function in the service.php file , do note that we donot need to remove the function parameter but the actual xml tag attribute that is mentioned inthe $rawpost variable

    regards,
    Omer Aslam

    Comment

    Working...