Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema API Commands for Solr 5+ #346

Closed
wants to merge 21 commits into from
Closed

Conversation

bpolaszek
Copy link
Contributor

https://cwiki.apache.org/confluence/display/solr/Schema+API

Add / replace / delete fields
Add / replace / delete dynamic fields
Add / replace / delete field types
Add / delete copy fields

Example usage:

<?php

use Solarium\Client;
use Solarium\QueryType\Schema\Query\Field\CopyField;
use Solarium\QueryType\Schema\Query\Field\Field;
use Solarium\QueryType\Schema\Query\FieldType\Analyzer\Tokenizer\Tokenizer;
use Solarium\QueryType\Schema\Query\FieldType\FieldType;

require_once __DIR__ . '/vendor/autoload.php';

$client             =   new Client($config);

# Create query
$schemaQuery        =   $client->createSchema();

# Create a new field type
$text_general_en    =   new FieldType('text_general_en', 'solr.TextField');

$indexAnalyzer      =   $text_general_en->createAnalyzer('index');
$indexAnalyzer      ->  setTokenizer(new Tokenizer('solr.WhitespaceTokenizerFactory'));
$indexAnalyzer      ->  createFilter('solr.ManagedStopFilterFactory')->addAttribute('managed', 'english_stopwords');

$queryAnalyzer      =   $text_general_en->createAnalyzer('query');
$queryAnalyzer      ->  setTokenizer(new Tokenizer('solr.WhitespaceTokenizerFactory'));
$queryAnalyzer      ->  createFilter('solr.ManagedSynonymFilterFactory', array('managed' => 'english_synonyms'));
$queryAnalyzer      ->  createFilter('solr.LowerCaseFilterFactory');

# Create a new field
$textField = new Field(array(
    'name'     => 'textField',
    'type'     => 'text_general_en',
    'required' => false,
    'stored'   => true,
    'indexed'  => true,
));

# Create a new dynamic field
$dynamicTextField = new Field(array(
    'name'     => '*__dtf',
    'type'     => 'text_general_en',
    'required' => false,
    'stored'   => true,
    'indexed'  => true,
));

# Copy *_dtf to field textField
$copyField          =   new CopyField('*__dtf', 'textField');

# Add all these instructions to the Schema Query
$schemaQuery    ->  addFieldType($text_general_en)
                ->  addField($textField)
                ->  addDynamicField($dynamicTextField)
                ->  addCopyField($copyField);

# Execute query
$client         ->  execute($schemaQuery);

You can also retrieve the Schema data when you don't attempt to modify it:

# Create query
$schemaQuery        =   $client->createSchema();

# Execute query
$schemaResponse     =   $client->execute($schemaQuery);

# Get field types
$fieldTypes         =   $schemaResponse->getFieldTypes();

# Get fields
$fields             =   $schemaResponse->getFields();

# Get dynamic Fields
$dynamicFields      =   $schemaResponse->getDynamicFields();

# Get copy Fields
$copyFields         =   $schemaResponse->getCopyFields();

# Get unique key
$uniqueKey          =   $schemaResponse->getUniqueKey();

# Get default search field
$defaultSearchField =   $schemaResponse->getDefaultSearchField();

This is my first PR ever, sorry if the git repo isn't totally cleaned up :/
Besides, even if I have tested most of use cases, some more accurate tests might be done to ensure this is production-ready.

@basdenooijer
Copy link
Member

Thanks a lot, this looks like it will be a great addition to the library!

I'll try to look into it with a bit more detail soon, and help with any remaining issues.
Unittests are missing, but that's also something I can help with.

However, in general this is a great first PR!

@bpolaszek
Copy link
Contributor Author

Hello,

Thanks for your reply :)
This is my first contribution to an open-source project, and I must admit I
don't practice unit testing enough to make a perfectly clean PR.
However, I tried to follow the main code guidelines and structures (I
started from the Update query), so things should be easy for you.

Besides, I did this for my own needs and I don't have enough time to allow
to an open-source project.
This also means, you have to make the code yours, because I won't be able
to take some time to solve issues on it in the future. Consider it as an
advanced draft of a missing feature.
Anyway, most of the work has been done, there should be just a little code
reformatting and testing. Maybe users should be warned that the Schema
queries only work on Solr 5+.

There's also something I may have messed up : I added the schema query in
the Solarium\Core\Client\Client class, but I just noticed my PhpStorm
settings reformatted the class with short-array syntax, that surely breaks
PHP 5.3 compatibility.

How can I fix it ? Do I have to reformat it again, delete this PR and make
another one ? Or is there another way ?

This leads me to another question : do you really want to keep PHP 5.3+
compatiblity ? PHP 5.3 is no longer officially supported since 2013.
Using the JsonSerialize interface introduced in PHP 5.4, traits, and
short-array syntax would have saved me lots of time :)

Thank you,
Ben

2015-07-02 7:59 GMT+02:00 Bas de Nooijer notifications@github.com:

Thanks a lot, this looks like it will be a great addition to the library!

I'll try to look into it with a bit more detail soon, and help with any
remaining issues.
Unittests are missing, but that's also something I can help with.

However, in general this is a great first PR!


Reply to this email directly or view it on GitHub
#346 (comment).

@bpolaszek
Copy link
Contributor Author

Hello again,

I fixed the Solarium\Core\Client\Client class by removing short-array syntax, giving it back its compatibility with PHP 5.3. The new commit is d28931c.

Ben

@shieldo
Copy link
Contributor

shieldo commented Nov 27, 2015

@bpolaszek Thanks for working on this! :)

@basdenooijer I'd like to help getting this to a mergeable state (tests/ formatting and so on). I don't know whether this would be by creating a different PR that contains these commits, with some additional ones on top? I think the branch will also need to be rebased against latest master.

@basdenooijer
Copy link
Member

@shieldo that would be great! I think creating a PR based on this one is indeed the way to go

@shieldo
Copy link
Contributor

shieldo commented Nov 30, 2015

Have opened #374.

@basdenooijer
Copy link
Member

Closing this PR in favor of #374

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants