0

I am working with solr 6.6.0 using solr PHP client. I am adding the docs using below code and it is working properly :

foreach ($data as $key => $value) {

                $docs['doc_no'.$i]['id'] = $value['id'];
                $docs['doc_no'.$i]['name'] = $value['name'];
                $docs['doc_no'.$i]['sub_title'] = strip_tags($value['sub_title']);
                $docs['doc_no'.$i]['small_image'] = $value['small_image'];
                $docs['doc_no'.$i]['project_type'] = $value['project_type'];
                $docs['doc_no'.$i]['project_status'] = $value['project_status'];
                $docs['doc_no'.$i]['logo'] = $value['logo'];
                $docs['doc_no'.$i]['price'] = $value['price'];
                $docs['doc_no'.$i]['url'] = $value['url'];
                $docs['doc_no'.$i]['flat_type_desc'] = $value['flat_type_desc'];
                $docs['doc_no'.$i]['project_config'] = $value['project_config'];
                $docs['doc_no'.$i]['address'] = $value['address'];
                $docs['doc_no'.$i]['location'] = $value['location'];

                $i++;
            }
            //print_r($docs);exit;

            $documents = array();
            foreach($docs as $item => $fields) {
                $part = new Apache_Solr_Document();

                foreach ( $fields as $key => $value ) {
                    if ( is_array( $value ) ) {
                        foreach ( $value as $data ) {
                            $part->setMultiValue( $key, $data );
                        }
                    }
                    else{
                        $part->$key = $value;
                    }
                }

                $documents[] = $part;
            }

            try {
                    $solr->addDocuments( $documents );
                    $solr->commit();
                    $solr->optimize();


                }
                catch ( Exception $e ) {
                    echo $e->getMessage();
                }

After executing the above code I have to manually restart the solr through cmd line and then it gets reflected, I want to ask that is every time when I add any docs in solr then I have to restart the solr manually ? Is there any other way to restart the solr automatically as soon as I have the data in docs.

Any help will be appreciated Thanks in advance.

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79

1 Answers1

0

For the submitted documents to be visible in the index, you have to issue a commit - and ask for a new reader to be opened (this is usually handled for you, so that's not usually necessary. How you do exactly that in the Drupal framework I have no idea about, but I'm guessing your Solr client has a commit method or something similar. I tried searching for the API docs, but came up empty except for the _Document class.

After a commit has been issued the index changes will be visible within a few seconds, or in the case of a soft commit (where the changes aren't persisted to disk before later) almost instantly.

You can also ask for a commitWithin interval when submitting documents, but that would also depend on how the client you're using works for how you include that parameter.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • thanks for your answer but as you see in my code I have written the commit code `$solr->commit();` – Rakesh Shetty Aug 15 '17 at 08:30
  • Ah, sorry. I'm not sure what the `$solr` client does when it sends `commit` - if it doesn't open a new searcher, you won't see the new documents. Also, there's usually no need for an optimize for each commit unless you're planning on letting the generated index live for a loong time (i.e. if new documents are submitted often, optimizing after each commit will be next to worthless). – MatsLindh Aug 15 '17 at 09:12
  • There is no need to optimize after _each and every commit_. Usually you don't have to call optimize yourself, as the mergeFactor will handle it as it becomes necessary, but if anything, calling optimize once a week should be more than enough if you need to evict deleted (and old versions of updated) documents. – MatsLindh Aug 15 '17 at 09:32
  • ok but still it not solve my real problem I have to manually restart my solr after adding data in docs – Rakesh Shetty Aug 15 '17 at 09:39
  • Which Solr client are you using? What does `$solr` refer to? It's hard to say how you should be using the client and its commit command without knowing what exactly that client is. – MatsLindh Aug 15 '17 at 09:41
  • According to that, commits already include the waitSearcher command, but you can also use the commitWithin option when adding documents - try that: `addDocuments($documents, $allowDups = false, $overwritePending = true, $overwriteCommitted = true, $commitWithin = 0)` – MatsLindh Aug 15 '17 at 09:48
  • did as you told ,still I have to restart the solr – Rakesh Shetty Aug 15 '17 at 10:02
  • Time to look at what the Solr log is saying when you're committing. – MatsLindh Aug 15 '17 at 10:05
  • where to see log? – Rakesh Shetty Aug 15 '17 at 10:07