Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
08d86ca8
Commit
08d86ca8
authored
Sep 28, 2011
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved spellcheck result models and added a spellcheck example script
parent
f8899d2b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
388 additions
and
50 deletions
+388
-50
examples/2.1.5.9-spellcheck.php
examples/2.1.5.9-spellcheck.php
+54
-0
examples/index.html
examples/index.html
+4
-3
library/Solarium/Client/ResponseParser/Select/Component/Spellcheck.php
...ium/Client/ResponseParser/Select/Component/Spellcheck.php
+96
-8
library/Solarium/Result/Select/Spellcheck.php
library/Solarium/Result/Select/Spellcheck.php
+54
-16
library/Solarium/Result/Select/Spellcheck/Collation.php
library/Solarium/Result/Select/Spellcheck/Collation.php
+48
-23
library/Solarium/Result/Select/Spellcheck/Suggestion.php
library/Solarium/Result/Select/Spellcheck/Suggestion.php
+132
-0
No files found.
examples/2.1.5.9-spellcheck.php
0 → 100644
View file @
08d86ca8
<?php
require
(
'init.php'
);
htmlHeader
();
// create a client instance
$client
=
new
Solarium_Client
(
$config
);
// get a select query instance
$query
=
$client
->
createSelect
();
$query
->
setRows
(
0
);
// add distributed search settings
// see http://wiki.apache.org/solr/DistributedSearch#Distributed_Search_Example for setting up two solr instances
$spellcheck
=
$query
->
getSpellcheck
();
$spellcheck
->
setQuery
(
'delll ultrashar'
);
$spellcheck
->
setBuild
(
true
);
$spellcheck
->
setCollate
(
true
);
$spellcheck
->
setExtendedResults
(
true
);
$spellcheck
->
setCollateExtendedResults
(
true
);
// this executes the query and returns the result
$resultset
=
$client
->
select
(
$query
);
$spellcheckResult
=
$resultset
->
getSpellcheck
();
echo
'<h1>Correctly spelled?</h1>'
;
if
(
$spellcheckResult
->
getCorrectlySpelled
())
{
echo
'yes'
;
}
else
{
echo
'no'
;
}
echo
'<h1>Suggestions</h1>'
;
foreach
(
$spellcheckResult
as
$suggestion
)
{
echo
'NumFound: '
.
$suggestion
->
getNumFound
()
.
'<br/>'
;
echo
'StartOffset: '
.
$suggestion
->
getStartOffset
()
.
'<br/>'
;
echo
'EndOffset: '
.
$suggestion
->
getEndOffset
()
.
'<br/>'
;
echo
'OriginalFrequency: '
.
$suggestion
->
getOriginalFrequency
()
.
'<br/>'
;
echo
'Frequency: '
.
$suggestion
->
getFrequency
()
.
'<br/>'
;
echo
'Word: '
.
$suggestion
->
getWord
()
.
'<br/>'
;
echo
'<hr/>'
;
}
$collation
=
$spellcheckResult
->
getCollation
();
echo
'<h1>Collation</h1>'
;
echo
'Query: '
.
$collation
->
getQuery
()
.
'<br/>'
;
echo
'Hits: '
.
$collation
->
getHits
()
.
'<br/>'
;
echo
'Corrections:<br/>'
;
foreach
(
$collation
->
getCorrections
()
as
$input
=>
$correction
)
{
echo
$input
.
' => '
.
$correction
.
'<br/>'
;
}
htmlFooter
();
\ No newline at end of file
examples/index.html
View file @
08d86ca8
...
...
@@ -50,9 +50,10 @@
</ul>
<li><a
href=
"2.1.5.4-dismax.php"
>
2.1.5.4 Dismax
</a></li>
<li><a
href=
"2.1.5.5-edismax.php"
>
2.1.5.5 Edismax
</a></li>
<li><a
href=
"2.1.5.6-grouping-by-field.php"
>
2.1.5.5 Grouping by field
</a></li>
<li><a
href=
"2.1.5.7-grouping-by-query.php"
>
2.1.5.6 Grouping by query
</a></li>
<li><a
href=
"2.1.5.8-distributed-search.php"
>
2.1.5.6 Distributed search (sharding)
</a></li>
<li><a
href=
"2.1.5.6-grouping-by-field.php"
>
2.1.5.6 Grouping by field
</a></li>
<li><a
href=
"2.1.5.7-grouping-by-query.php"
>
2.1.5.7 Grouping by query
</a></li>
<li><a
href=
"2.1.5.8-distributed-search.php"
>
2.1.5.8 Distributed search (sharding)
</a></li>
<li><a
href=
"2.1.5.9-spellcheck.php"
>
2.1.5.9 Spellcheck
</a></li>
</ul>
<li><a
href=
"2.1.6-helper-functions.php"
>
2.1.6 Helper functions
</a></li>
<li><a
href=
"2.1.7-query-reuse.php"
>
2.1.7 Query re-use
</a></li>
...
...
library/Solarium/Client/ResponseParser/Select/Component/Spellcheck.php
View file @
08d86ca8
...
...
@@ -51,21 +51,109 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_Spellcheck $spellcheck
* @param array $data
* @return Solarium_Result_Select_Spellcheck
* @return Solarium_Result_Select_Spellcheck
|null
*/
public
function
parse
(
$query
,
$spellcheck
,
$data
)
{
$results
=
array
();
if
(
isset
(
$data
[
'spellcheck'
]
)
)
{
if
(
isset
(
$data
[
'spellcheck'
]
[
'suggestions'
])
&&
is_array
(
$data
[
'spellcheck'
][
'suggestions'
])
&&
count
(
$data
[
'spellcheck'
][
'suggestions'
])
>
0
)
{
$spellcheckResults
=
$data
[
'spellcheck'
];
foreach
(
$spellcheckResults
AS
$key
=>
$result
)
{
$results
[
$key
]
=
new
Solarium_Result_Select_Spellcheck_Result
(
$result
);
$spellcheckResults
=
$data
[
'spellcheck'
][
'suggestions'
];
$suggestions
=
array
();
$correctlySpelled
=
null
;
$collation
=
null
;
$index
=
0
;
while
(
isset
(
$spellcheckResults
[
$index
])
&&
isset
(
$spellcheckResults
[
$index
+
1
]))
{
$key
=
$spellcheckResults
[
$index
];
$value
=
$spellcheckResults
[
$index
+
1
];
switch
(
$key
)
{
case
'correctlySpelled'
:
$correctlySpelled
=
$value
;
break
;
case
'collation'
:
$collation
=
$this
->
_parseCollation
(
$value
);
break
;
default
:
$suggestions
[]
=
$this
->
_parseSuggestion
(
$key
,
$value
);
}
$index
+=
2
;
}
return
new
Solarium_Result_Select_Spellcheck
(
$suggestions
,
$collation
,
$correctlySpelled
);
}
else
{
return
null
;
}
}
protected
function
_parseCollation
(
$values
)
{
if
(
is_string
(
$values
))
{
return
new
Solarium_Result_Select_Spellcheck_Collation
(
$values
,
null
,
array
());
}
else
{
$query
=
null
;
$hits
=
null
;
$correctionResult
=
null
;
$index
=
0
;
while
(
isset
(
$values
[
$index
])
&&
isset
(
$values
[
$index
+
1
]))
{
$key
=
$values
[
$index
];
$value
=
$values
[
$index
+
1
];
switch
(
$key
)
{
case
'collationQuery'
:
$query
=
$value
;
break
;
case
'hits'
:
$hits
=
$value
;
break
;
case
'misspellingsAndCorrections'
:
$correctionResult
=
$value
;
break
;
}
$index
+=
2
;
}
$corrections
=
array
();
if
(
$correctionResult
!==
null
)
{
$index
=
0
;
while
(
isset
(
$correctionResult
[
$index
])
&&
isset
(
$correctionResult
[
$index
+
1
]))
{
$input
=
$correctionResult
[
$index
];
$correction
=
$correctionResult
[
$index
+
1
];
$corrections
[
$input
]
=
$correction
;
$index
+=
2
;
}
}
return
new
Solarium_Result_Select_Spellcheck_Collation
(
$query
,
$hits
,
$corrections
);
}
}
protected
function
_parseSuggestion
(
$key
,
$value
)
{
$numFound
=
(
isset
(
$value
[
'numFound'
]))
?
$value
[
'numFound'
]
:
null
;
$startOffset
=
(
isset
(
$value
[
'startOffset'
]))
?
$value
[
'startOffset'
]
:
null
;
$endOffset
=
(
isset
(
$value
[
'endOffset'
]))
?
$value
[
'endOffset'
]
:
null
;
$originalFrequency
=
(
isset
(
$value
[
'origFreq'
]))
?
$value
[
'origFreq'
]
:
null
;
if
(
is_string
(
$value
[
'suggestion'
][
0
]))
{
$word
=
$value
[
'suggestion'
][
0
];
$frequency
=
null
;
}
else
{
$word
=
$value
[
'suggestion'
][
0
][
'word'
];
$frequency
=
$value
[
'suggestion'
][
0
][
'freq'
];
}
return
new
Solarium_Result_Select_Spellcheck
(
$results
);
return
new
Solarium_Result_Select_Spellcheck_Suggestion
(
$numFound
,
$startOffset
,
$endOffset
,
$originalFrequency
,
$word
,
$frequency
);
}
}
\ No newline at end of file
library/Solarium/Result/Select/Spellcheck.php
View file @
08d86ca8
...
...
@@ -46,48 +46,86 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
{
/**
*
Result
array
*
Suggestions
array
*
* @var array
*/
protected
$_results
;
protected
$_suggestions
;
/**
* Collation object
*
* @var Solarium_Result_Select_Spellcheck_Collation
*/
protected
$_collation
;
/**
* @var boolean
*/
protected
$_correctlySpelled
;
/**
* Constructor
*
* @param array $results
* @param array $suggestions
* @param Solarium_Result_Select_Spellcheck_Collation $collation
* @param boolean $correctlySpelled
* @return void
*/
public
function
__construct
(
$results
)
public
function
__construct
(
$suggestions
,
$collation
,
$correctlySpelled
)
{
$this
->
_suggestions
=
$suggestions
;
$this
->
_collation
=
$collation
;
$this
->
_correctlySpelled
=
$correctlySpelled
;
}
/**
* Get the collation result
*
* @return Solarium_Result_Select_Spellcheck_Collation
*/
public
function
getCollation
()
{
$this
->
_results
=
$results
;
return
$this
->
_collation
;
}
/**
* Get correctly spelled status
*
* Only available if ExtendedResults was enabled in your query
*
* @return bool
*/
public
function
getCorrectlySpelled
()
{
return
$this
->
_correctlySpelled
;
}
/**
* Get a result by key
*
* @param mixed $key
* @return Solarium_Result_Select_Highlighting_
Result
|null
* @return Solarium_Result_Select_Highlighting_
Suggestion
|null
*/
public
function
get
Result
(
$key
)
public
function
get
Suggestion
(
$key
)
{
if
(
isset
(
$this
->
_
result
s
[
$key
]))
{
return
$this
->
_
result
s
[
$key
];
if
(
isset
(
$this
->
_
suggestion
s
[
$key
]))
{
return
$this
->
_
suggestion
s
[
$key
];
}
else
{
return
null
;
}
}
/**
* Get all
result
s
* Get all
suggestion
s
*
* @return array
*/
public
function
get
Result
s
()
public
function
get
Suggestion
s
()
{
return
$this
->
_
result
s
;
return
$this
->
_
suggestion
s
;
}
/**
* IteratorAggregate implementation
*
...
...
@@ -95,7 +133,7 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
*/
public
function
getIterator
()
{
return
new
ArrayIterator
(
$this
->
_
result
s
);
return
new
ArrayIterator
(
$this
->
_
suggestion
s
);
}
/**
...
...
@@ -105,6 +143,6 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
*/
public
function
count
()
{
return
count
(
$this
->
_
result
s
);
return
count
(
$this
->
_
suggestion
s
);
}
}
\ No newline at end of file
library/Solarium/Result/Select/Spellcheck/
Result
.php
→
library/Solarium/Result/Select/Spellcheck/
Collation
.php
View file @
08d86ca8
...
...
@@ -37,73 +37,98 @@
*/
/**
* Select component spellcheck
result item
* Select component spellcheck
collation result
*
* @package Solarium
* @subpackage Result
*/
class
Solarium_Result_Select_Spellcheck_
Result
implements
IteratorAggregate
,
Countable
class
Solarium_Result_Select_Spellcheck_
Collation
implements
IteratorAggregate
,
Countable
{
/**
* Fields array
*
* @var string
*/
protected
$_query
;
/**
* @var int
*/
protected
$_hits
;
/**
* @var array
*/
protected
$_
field
s
;
protected
$_
correction
s
;
/**
* Constructor
*
* @param array $fields
* @return void
* @param string $query
* @param int|null $hits
* @param array $corrections
*/
public
function
__construct
(
$
field
s
)
public
function
__construct
(
$
query
,
$hits
,
$correction
s
)
{
$this
->
_fields
=
$fields
;
$this
->
_query
=
$query
;
$this
->
_hits
=
$hits
;
$this
->
_corrections
=
$corrections
;
}
/**
* Get
highlights for all fields
* Get
query string
*
* @return
array
* @return
string
*/
public
function
get
Fields
()
public
function
get
Query
()
{
return
$this
->
_
fields
;
return
$this
->
_
query
;
}
/**
* Get highlights for a single field
* Get hit count
*
* Only available if ExtendedResults was enabled in your query
*
* @return int|null
*/
public
function
getHits
()
{
return
$this
->
_hits
;
}
/**
* Get all corrrections
*
* Only available if ExtendedResults was enabled in your query
*
* @return array
*/
public
function
get
Field
(
$key
)
public
function
get
Corrections
(
)
{
if
(
isset
(
$this
->
_fields
[
$key
]))
{
return
$this
->
_fields
[
$key
];
}
else
{
return
array
();
}
return
$this
->
_corrections
;
}
/**
* IteratorAggregate implementation
*
* Only available if ExtendedResults was enabled in your query
*
* @return ArrayIterator
*/
public
function
getIterator
()
{
return
new
ArrayIterator
(
$this
->
_
field
s
);
return
new
ArrayIterator
(
$this
->
_
correction
s
);
}
/**
* Countable implementation
*
* Only available if ExtendedResults was enabled in your query
*
* @return int
*/
public
function
count
()
{
return
count
(
$this
->
_
field
s
);
return
count
(
$this
->
_
correction
s
);
}
}
\ No newline at end of file
library/Solarium/Result/Select/Spellcheck/Suggestion.php
0 → 100644
View file @
08d86ca8
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component spellcheck suggestion result
*
* @package Solarium
* @subpackage Result
*/
class
Solarium_Result_Select_Spellcheck_Suggestion
{
/**
* Constructor
*
* @param int $numFound
* @param int $startOffset
* @param int $endOffset
* @param int $originalFrequency
* @param string $word
* @param int $frequency
*/
public
function
__construct
(
$numFound
,
$startOffset
,
$endOffset
,
$originalFrequency
,
$word
,
$frequency
)
{
$this
->
_numFound
=
$numFound
;
$this
->
_startOffset
=
$startOffset
;
$this
->
_endOffset
=
$endOffset
;
$this
->
_originalFrequency
=
$originalFrequency
;
$this
->
_word
=
$word
;
$this
->
_frequency
=
$frequency
;
}
/**
* Get numFound value
*
* @return int
*/
public
function
getNumFound
()
{
return
$this
->
_numFound
;
}
/**
* Get startOffset value
*
* @return int
*/
public
function
getStartOffset
()
{
return
$this
->
_startOffset
;
}
/**
* Get endOffset value
*
* @return int
*/
public
function
getEndOffset
()
{
return
$this
->
_endOffset
;
}
/**
* Get originalFrequency value
*
* Only available if CollateExtendedResults was enabled in your query
*
* @return int
*/
public
function
getOriginalFrequency
()
{
return
$this
->
_originalFrequency
;
}
/**
* Get word
*
* @return string
*/
public
function
getWord
()
{
return
$this
->
_word
;
}
/**
* Get frequency value
*
* Only available if CollateExtendedResults was enabled in your query
*
* @return int
*/
public
function
getFrequency
()
{
return
$this
->
_frequency
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment