Skip to content

Commit

Permalink
Add separate Elasticsearch types for reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
dstillman committed Nov 22, 2013
1 parent fec0585 commit dd0a523
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
8 changes: 6 additions & 2 deletions misc/elasticsearch/aliases.json-sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"actions": [
{
"add": {
"alias": "item_fulltext_index",
"index": "item_fulltext_index_v1"
"alias": "item_fulltext_index_read",
"index": "item_fulltext_index_a"
},
"add": {
"alias": "item_fulltext_index_write",
"index": "item_fulltext_index_a"
}
}
]
Expand Down
10 changes: 5 additions & 5 deletions misc/elasticsearch/bin/init
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/bin/bash

if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 host index_version"
echo "Usage: $0 host index_suffix"
exit
fi
HOST="$1"
INDEX_VERSION="$2"
INDEX_SUFFIX="$2"

# Get path to script's parent directory
DIR=$(cd ${0%/*} && echo $PWD/${0##*/})
DIR=`dirname $DIR`
DIR=`dirname $DIR`
ITEM_FULLTEXT_DIR=$DIR/item_fulltext

curl -XPUT "http://$HOST:9200/item_fulltext_index_v$INDEX_VERSION" -d @$ITEM_FULLTEXT_DIR/init.json
curl -XPUT "http://$HOST:9200/item_fulltext_index_$INDEX_SUFFIX" -d @$ITEM_FULLTEXT_DIR/init.json
echo

$DIR/bin/update-settings $HOST $INDEX_VERSION
$DIR/bin/update-settings $HOST $INDEX_SUFFIX

curl -XPUT "http://$HOST:9200/item_fulltext_index_v$INDEX_VERSION/item_fulltext/_mapping" -d @$ITEM_FULLTEXT_DIR/mapping.json
curl -XPUT "http://$HOST:9200/item_fulltext_index_$INDEX_SUFFIX/item_fulltext/_mapping" -d @$ITEM_FULLTEXT_DIR/mapping.json
echo

curl -XPOST "http://$HOST:9200/_aliases" -d @aliases.json
Expand Down
15 changes: 15 additions & 0 deletions misc/elasticsearch/bin/update-aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

if [ -z "$1" ]; then
echo "Usage: $0 host"
exit
fi
HOST="$1"

# Get path to script's parent directory
DIR=$(cd ${0%/*} && echo $PWD/${0##*/})
DIR=`dirname $DIR`
DIR=`dirname $DIR`

curl -XPOST "http://$HOST:9200/_aliases" -d @$DIR/aliases.json
echo
6 changes: 3 additions & 3 deletions misc/elasticsearch/bin/update-settings
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/sh

if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 host index_version"
echo "Usage: $0 host index_suffix"
exit
fi
HOST="$1"
INDEX_VERSION="$2"
INDEX_SUFFIX="$2"

# Get path to script's parent directory
DIR=$(cd ${0%/*} && echo $PWD/${0##*/})
DIR=`dirname $DIR`
DIR=`dirname $DIR`
ITEM_FULLTEXT_DIR=$DIR/item_fulltext

curl -XPUT "http://$HOST:9200/item_fulltext_index_v$INDEX_VERSION/_settings" -d @$ITEM_FULLTEXT_DIR/settings.json
curl -XPUT "http://$HOST:9200/item_fulltext_index_$INDEX_SUFFIX/_settings" -d @$ITEM_FULLTEXT_DIR/settings.json
echo
29 changes: 19 additions & 10 deletions model/FullText.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function indexItem(Zotero_Item $item, $content, $stats=array()) {

// Add to Elasticsearch
try {
$type = self::getType();
$type = self::getWriteType();

$id = $libraryID . "/" . $key;
$doc = [
Expand Down Expand Up @@ -160,8 +160,7 @@ public static function searchInLibrary($libraryID, $searchText) {
// TEMP: For now, strip double-quotes and make everything a phrase search
$searchText = str_replace('"', '', $searchText);

$index = self::getIndex();
$type = self::getType();
$type = self::getReadType();

$libraryFilter = new \Elastica\Filter\Term();
$libraryFilter->setTerm("libraryID", $libraryID);
Expand Down Expand Up @@ -200,8 +199,7 @@ public static function deleteItemContent(Zotero_Item $item) {

// Delete from Elasticsearch
try {
$index = self::getIndex();
$type = self::getType();
$type = self::getWriteType();

try {
$response = $type->deleteById($libraryID . "/" . $key);
Expand Down Expand Up @@ -235,7 +233,7 @@ public static function deleteByLibrary($libraryID) {

// Delete from Elasticsearch
try {
$type = self::getType();
$type = self::getWriteType();

$libraryQuery = new \Elastica\Query\Term();
$libraryQuery->setTerm("libraryID", $libraryID);
Expand Down Expand Up @@ -299,12 +297,23 @@ public static function itemDataToXML($data, DOMDocument $doc, $empty=false) {
return $xmlNode;
}

private static function getIndex() {
return Z_Core::$Elastica->getIndex(self::$elasticsearchType . "_index");

private static function getReadIndex() {
return Z_Core::$Elastica->getIndex(self::$elasticsearchType . "_index_read");
}


private static function getWriteIndex() {
return Z_Core::$Elastica->getIndex(self::$elasticsearchType . "_index_write");
}


private static function getReadType() {
return new \Elastica\Type(self::getReadIndex(), self::$elasticsearchType);
}


private static function getType() {
return new \Elastica\Type(self::getIndex(), self::$elasticsearchType);
private static function getWriteType() {
return new \Elastica\Type(self::getWriteIndex(), self::$elasticsearchType);
}
}

0 comments on commit dd0a523

Please sign in to comment.