Skip to content

Commit

Permalink
Move index remove into indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
l3l0 authored and tyomo4ka committed Jun 8, 2012
1 parent dadd729 commit 1ff02a1
Show file tree
Hide file tree
Showing 26 changed files with 6,111 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/web/sitemap.xml*
/app/DoctrineMigrations/*
/src/Knp/Bundle/KnpBundlesBundle/DoctrineMigrations/*
/src/Knp/Bundle/KnpBundlesBundle/Resources/solr/data/*
/vendor/*
/app/config/parameters.yml
/app/config/parameters.*.yml
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ before_script:
- cp app/config/parameters.yml.test app/config/parameters.yml
- php bin/vendors install
- ./bin/prepare-test-env.sh
- ./bin/prepare-test-solr.sh

script: phpunit -c app/ && php app/console --env=test behat --format progress

Expand Down
8 changes: 6 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ so the commands needs to wait.

We use [Solr](http://lucene.apache.org/solr/) and it's PHP client [Solarium](http://solarium-project.org) to search bundles.
Recommended schema can be found
[**here**](https://github.com/KnpLabs/KnpBundles/blob/master/src/Knp/Bundle/KnpBundlesBundle/Resources/solr/schema.xml).
Put it inside the `solr/conf` directory, and run Solr with `$ java -jar start.jar`.
[**here**](https://github.com/KnpLabs/KnpBundles/blob/master/src/Knp/Bundle/KnpBundlesBundle/Resources/solr/conf/schema.xml).
You can run SOLR using:

php app/console kb:solr:start

See bin/prepare-test-solr.sh script
Bundles will be automatically indexed on next update, or you can force indexing by console command.

If you have Solr up and running, simply do:
Expand Down
2 changes: 1 addition & 1 deletion app/config/parameters.yml.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parameters:
kb_sitemap.base_url: http://knpbundles.com

solarium.host: 127.0.0.1
solarium.port: 8983
solarium.port: 8984
solarium.path: /solr

banner:
Expand Down
4 changes: 4 additions & 0 deletions bin/prepare-test-solr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wget http://www.idg.pl/mirrors/apache/lucene/solr/3.6.0/apache-solr-3.6.0.tgz
tar -zxvf apache-solr-3.6.0.tgz
mv apache-solr-3.6.0 apache-solr
php app/console --env=test kb:solr:start --solr-path="`pwd`/apache-solr/example/"
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundleName = $input->getArgument('bundleName');

$doctrine = $this->getContainer()->get('doctrine');
$solarium = $this->getContainer()->get('solarium.client');
$indexer = $this->getContainer()->get('knp_bundles.indexer.solr');

if ($bundleName) {
Expand All @@ -56,12 +55,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($verbose) {
$output->writeln('Deleting existing index.');
}

$update = $solarium->createUpdate();
$update->addDeleteQuery('*:*');
$update->addCommit();

$solarium->update($update);

$indexer->deleteBundlesIndexes();
}

foreach ($bundles as $bundle) {
Expand Down
118 changes: 118 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Command/KbSolrServerStartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Knp\Bundle\KnpBundlesBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

/**
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
*/
class KbSolrServerStartCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('kb:solr:start')
->setDescription('Start SOLR for given enviroment')
->addOption('solr-path', 'p', InputOption::VALUE_OPTIONAL, 'path to solr (where start.jar is localized)', '/opt/solr/example')
->addOption('show-commands-only', null, InputOption::VALUE_NONE, 'If set show command but not execute it')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('show-commands-only')) {
$output->writeln(sprintf('<info>%s</info>', $this->createRunSolrCommand($input)));

return 0;
}

if ($this->solrIsRunning()) {
$output->writeln(sprintf('<info>%s %d</info>', 'Solr is running. Pid: ', $this->getSolrPid()));

return 0;
}

$output->writeln(sprintf('<info>%s</info>', 'Starting solr in background process'));

$process = new Process($this->createRunSolrCommand($input));
$process->run();

$output->writeln(sprintf('<info>Pid: %d</info>', $this->getSolrPid()));
}

/**
* @return boolean
*/
private function solrIsRunning()
{
return (boolean) $this->getSolrPid();
}

/**
* Get SOLR pid
*/
private function getSolrPid()
{
$properties = array();
foreach ($this->getPropertiesArray() as $key => $property) {
$properties[] = $key.'='.$property;
}

$process = new Process(sprintf('ps aux | grep \\\\%s | grep -v grep | awk \'{ print $2 }\'', implode('| grep \\\\', $properties)));
$process->run();
$pid = $process->getOutput();

return (integer) $pid;
}

/**
* Create and return SOLR start command
*
* @param InputInterface $input
* @return string
*/
private function createRunSolrCommand(InputInterface $input)
{
$solrPath = $input->getOption('solr-path');

return sprintf('(cd %s; java -jar %s start.jar ) 1> /dev/null 2> /dev/null &', $solrPath, $this->buildProperties());
}

/**
* Build SOLR start.jar properties
*
* @return string
*/
private function buildProperties()
{
$properties = array();
foreach ($this->getPropertiesArray() as $key => $property) {
$properties[] = $key.'='.$property;
}

return implode(' ', $properties);
}

/**
* @return array
*/
private function getPropertiesArray()
{
return array(
'-Djetty.port' => $this->getContainer()->get('solarium.client')->getAdapter()->getPort(),
'-Dsolr.solr.home' => $this->getContainer()->get('kernel')->getBundle('KnpBundlesBundle')->getPath().'/Resources/solr'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public function searchAction()

$format = $this->recognizeRequestFormat();

if ('html' === $format && count($bundles) === 1 && strtolower($bundles[0]['name']) == strtolower($query)) {
$params = array('username' => $bundles[0]['username'], 'name' => $bundles[0]['name']);

return $this->redirect($this->generateUrl('bundle_show', $params));
}

return $this->render('KnpBundlesBundle:Bundle:searchResults.'.$format.'.twig', array(
'query' => urldecode($this->get('request')->query->get('q')),
'bundles' => $bundles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class FeatureContext extends MinkContext
public function __construct($kernel)
{
$this->useContext('symfony_doctrine', new \Behat\CommonContexts\SymfonyDoctrineContext($kernel));
$this->useContext('solr', new \Knp\Bundle\KnpBundlesBundle\Features\Context\SolrContext($kernel));

parent::__construct($kernel);
}
Expand Down
45 changes: 45 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Features/Context/SolrContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Knp\Bundle\KnpBundlesBundle\Features\Context;

use Behat\BehatBundle\Context\BehatContext;

require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';

/**
* Solr context.
*/
class SolrContext extends BehatContext
{
/**
* @Given /^bundles are indexed$/
*/
public function bundlesAreIndexed()
{
$this->solrIsEnabled();
$bundles = $this->getContainer()->get('doctrine')->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Bundle')->findAll();

$indexer = $this->getContainer()->get('knp_bundles.indexer.solr');
$indexer->deleteBundlesIndexes();
foreach ($bundles as $bundle) {
$indexer->indexBundle($bundle);
}
}

/**
* @throw Solr HTTP error
*/
protected function solrIsEnabled()
{
$client = $this->getSolariumClient();
$query = $client->createPing();

$client->ping($query);
}

protected function getSolariumClient()
{
return $this->getContainer()->get('solarium.client');
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@solr
Feature: Listing and searching bundles by keywords
As a Visitor
I want to list or search bundles by keywords
Expand All @@ -19,6 +20,7 @@ Feature: Listing and searching bundles by keywords
| Test2Bundle | unique |
| UserBundle | user |
| UserBundle | user login |
And bundles are indexed

Scenario: List bundles by keyword
When I go to "/keyword/test"
Expand Down
12 changes: 3 additions & 9 deletions src/Knp/Bundle/KnpBundlesBundle/Features/bundleSearch.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@solr
Feature: Searching bundles
As a Visitor
I want to search bundles
Expand All @@ -7,19 +8,12 @@ Feature: Searching bundles
| name |
| knplabs |
| fos |
Given the site has following bundles:
And the site has following bundles:
| username | name | description | lastCommitAt | score | trend1 |
| knplabs | TestBundle | test desc |-1 day | 10 | 15 |
| knplabs | Test2Bundle | test desc |-1 day | 10 | 15 |
| fos | UserBundle | user desc |-2 days | 20 | 5 |

Scenario: Searching all bundles
When I go to "/"
And I search for "Bundle"
Then I should see "3 Bundles"
And I should see "TestBundle"
And I should see "Test2Bundle"
And I should see "UserBundle"
And bundles are indexed

Scenario: Searching some bundles
When I go to "/"
Expand Down
12 changes: 12 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Indexer/SolrIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ private function updateDocumentFromBundle(\Solarium_Document_ReadWrite $document
}
$document->keywords = $keywords;
}

/**
* Delete all bundles from index
*/
public function deleteBundlesIndexes()
{
$delete = $this->solarium->createUpdate();
$delete->addDeleteQuery('*:*');
$delete->addCommit();

$this->solarium->update($delete);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- The content of this page will be statically included into the top
of the admin page. Uncomment this as an example to see there the content
will show up.
<hr>
<i>This line will appear before the first table</i>
<tr>
<td colspan="2">
This row will be appended to the end of the first table
</td>
</tr>
<hr>
-->
36 changes: 36 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Resources/solr/conf/elevate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- If this file is found in the config directory, it will only be
loaded once at startup. If it is found in Solr's data
directory, it will be re-loaded every commit.
-->

<elevate>
<query text="foo bar">
<doc id="1" />
<doc id="2" />
<doc id="3" />
</query>

<query text="ipod">
<doc id="MA147LL/A" /> <!-- put the actual ipod at the top -->
<doc id="IW-02" exclude="true" /> <!-- exclude this cable -->
</query>

</elevate>
Loading

0 comments on commit 1ff02a1

Please sign in to comment.