Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2a98bb30
Commit
2a98bb30
authored
Jun 05, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs for Sphinx extension updated
parent
520d295b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
8 deletions
+70
-8
Query.php
extensions/sphinx/Query.php
+2
-2
README.md
extensions/sphinx/README.md
+68
-6
No files found.
extensions/sphinx/Query.php
View file @
2a98bb30
...
...
@@ -386,12 +386,12 @@ class Query extends Component implements QueryInterface
* MATCH operator inside the WHERE clause.
* Note: this value will be processed by [[Connection::escapeMatchValue()]],
* if you need to compose complex match condition use [[Expression]]:
*
```
*
~~~
* $query = new Query;
* $query->from('my_index')
* ->match(new Expression(':match', ['match' => '@(content) ' . Yii::$app->sphinx->escapeMatchValue($matchValue)]))
* ->all();
*
```
*
~~~
*
* @param string $query fulltext query text.
* @return static the query object itself
...
...
extensions/sphinx/README.md
View file @
2a98bb30
...
...
@@ -120,12 +120,6 @@ $rows = $query->from('idx_item')
escape any special characters, which may break the query.
Snippets
--------
TODO
Using the ActiveRecord
----------------------
...
...
@@ -181,3 +175,71 @@ $provider = new ActiveDataProvider([
]);
$models
=
$provider
->
getModels
();
```
Building Snippets (Excerpts)
----------------------------
Snippet (excerpt) - is a fragment of the index source text, which contains highlighted words from fulltext search
condition. Sphinx has a powerful build-in mechanism to compose snippets. However, since Sphinx does not store the
original indexed text, the snippets for the rows in query result should be build separately via another query.
Such query may be performed via
`yii\sphinx\Command::callSnippets()`
:
```
php
$sql
=
"SELECT * FROM idx_item WHERE MATCH('about')"
;
$rows
=
Yii
::
$app
->
sphinx
->
createCommand
(
$sql
)
->
queryAll
();
$rowSnippetSources
=
[];
foreach
(
$rows
as
$row
)
{
$rowSnippetSources
[]
=
file_get_contents
(
'/path/to/index/files/'
.
$row
[
'id'
]
.
'.txt'
);
}
$snippets
=
Yii
::
$app
->
sphinx
->
createCommand
(
$sql
)
->
callSnippets
(
'idx_item'
,
$rowSnippetSources
,
'about'
);
```
You can simplify this workflow using
[
[yii\sphinx\Query::snippetCallback
]
].
It is a PHP callback, which receives array of query result rows as an argument and must return the
array of snippet source strings in the order, which match one of incoming rows.
Example:
```
php
use
yii\sphinx\Query
;
$query
=
new
Query
;
$rows
=
$query
->
from
(
'idx_item'
)
->
match
(
$_POST
[
'search'
])
->
snippetCallback
(
function
(
$rows
)
{
$result
=
[];
foreach
(
$rows
as
$row
)
{
$result
[]
=
file_get_contents
(
'/path/to/index/files/'
.
$row
[
'id'
]
.
'.txt'
);
}
return
$result
;
})
->
all
();
foreach
(
$rows
as
$row
)
{
echo
$row
[
'snippet'
];
}
```
If you are using Active Record, you can
[
[yii\sphinx\ActiveQuery::snippetByModel()
]
] to compose a snippets.
This method retrieves snippet source per each row calling
`getSnippetSource()`
method of the result model.
All you need to do is implement it in your Active Record class, so it return the correct value:
```
php
use
yii\sphinx\ActiveRecord
;
class
Article
extends
ActiveRecord
{
public
function
getSnippetSource
()
{
return
file_get_contents
(
'/path/to/source/files/'
.
$this
->
id
.
'.txt'
);;
}
}
$articles
=
Article
::
find
()
->
snippetByModel
()
->
all
();
foreach
(
$articles
as
$article
)
{
echo
$article
->
snippet
;
}
```
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