Skip to content

Commit

Permalink
Analyzers are configurable from the design document
Browse files Browse the repository at this point in the history
 - Views are reindexed if configuration changes
 - No need for database-awareness
  • Loading branch information
rgabo committed Jan 13, 2011
1 parent 5dd5e4c commit cd63a98
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 45 deletions.
Expand Up @@ -379,7 +379,7 @@ public Void handleResponse(final HttpResponse response)
for (final Entry<View, IndexState> entry : states.entrySet()) {
final View view = entry.getKey();
final IndexState state = entry.getValue();
final Analyzer analyzer = view.getAnalyzer(database);
final Analyzer analyzer = view.getAnalyzer();
final Similarity similarity = view.getSimilarity();

state.writer.setSimilarity(similarity);
Expand Down Expand Up @@ -826,8 +826,8 @@ private void init() throws IOException, JSONException {
.entrySet()) {
final String name = entry.getKey();
final View view = entry.getValue();
final Analyzer analyzer = view.getAnalyzer(database);
final Analyzer queryAnalyzer = view.getQueryAnalyzer(database);
final Analyzer analyzer = view.getAnalyzer();
final Analyzer queryAnalyzer = view.getQueryAnalyzer();
final Similarity similarity = view.getSimilarity();

paths.put(toPath(ddoc.getId(), name), view);
Expand Down

This file was deleted.

Expand Up @@ -2,12 +2,12 @@

/**
* Copyright 2010 Robert Newson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Licensed 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
* 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,
Expand All @@ -34,7 +34,7 @@ public DesignDocument(final JSONObject json) throws JSONException {
throw new IllegalArgumentException(json
+ " is not a design document");
}
fulltext = json.optJSONObject("fulltext");
this.fulltext = json.optJSONObject("fulltext");
}

public DesignDocument(final CouchDocument doc) throws JSONException {
Expand All @@ -44,8 +44,8 @@ public DesignDocument(final CouchDocument doc) throws JSONException {
public View getView(final String name) {
if (fulltext == null)
return null;
final JSONObject json = fulltext.optJSONObject(name);
return json == null ? null : new View(json);
final JSONObject viewJson = fulltext.optJSONObject(name);
return viewJson == null ? null : new View(viewJson);
}

public Map<String, View> getAllViews() {
Expand Down
49 changes: 28 additions & 21 deletions src/main/java/com/github/rnewson/couchdb/lucene/couchdb/View.java
Expand Up @@ -31,6 +31,7 @@
import org.mozilla.javascript.ScriptableObject;

import com.github.rnewson.couchdb.lucene.util.Analyzers;
import com.github.rnewson.couchdb.lucene.util.Configurable;
import com.github.rnewson.couchdb.lucene.util.Similarities;

public final class View {
Expand All @@ -40,9 +41,10 @@ public final class View {

private static final String ANALYZER = "analyzer";
private static final String QUERY_ANALYZER = "query_analyzer";
private static final String SIMILARITY= "similarity";
private static final String SIMILARITY = "similarity";
private static final String INDEX = "index";
private static final String DEFAULTS = "defaults";
private static final String CONFIGURATION = "configuration";

private final JSONObject json;

Expand All @@ -53,38 +55,40 @@ public View(final JSONObject json) {
this.json = json;
}

public Analyzer getAnalyzer(Database database) throws JSONException {
Analyzer analyzer = Analyzers.getAnalyzer(json.optString(ANALYZER, DEFAULT_ANALYZER));
if (database != null && analyzer instanceof Databasable) {
((Databasable) analyzer).setDatabase(database);
}
return analyzer;
}

public Analyzer getAnalyzer() throws JSONException {
return getAnalyzer(null);
}

public Analyzer getQueryAnalyzer(Database database) throws JSONException {
Analyzer analyzer = Analyzers.getAnalyzer(json.optString(QUERY_ANALYZER, json.optString(ANALYZER, DEFAULT_ANALYZER)));
if (database != null && analyzer instanceof Databasable) {
((Databasable) analyzer).setDatabase(database);
Analyzer analyzer = Analyzers.getAnalyzer(json.optString(ANALYZER,
DEFAULT_ANALYZER));
if (analyzer instanceof Configurable) {
((Configurable) analyzer).setConfiguration(json
.optJSONObject(CONFIGURATION));
}
return analyzer;
}

public Analyzer getQueryAnalyzer() throws JSONException {
return getAnalyzer(null);
Analyzer analyzer = Analyzers.getAnalyzer(json.optString(
QUERY_ANALYZER, json.optString(ANALYZER, DEFAULT_ANALYZER)));
if (analyzer instanceof Configurable) {
((Configurable) analyzer).setConfiguration(json
.optJSONObject(CONFIGURATION));
}
return analyzer;
}

public Similarity getSimilarity() throws JSONException {
return Similarities
.getSimilarity(json.optString(SIMILARITY, DEFAULT_SIMILARITY));
Similarity similarity = Similarities.getSimilarity(json.optString(
SIMILARITY, DEFAULT_SIMILARITY));
if (similarity instanceof Configurable) {
((Configurable) similarity).setConfiguration(json
.optJSONObject(CONFIGURATION));
}
return similarity;
}

public ViewSettings getDefaultSettings() throws JSONException {
return json.has(DEFAULTS) ? new ViewSettings(json
.getJSONObject(DEFAULTS)) : ViewSettings.getDefaultSettings();
return json.has(DEFAULTS) ? new ViewSettings(
json.getJSONObject(DEFAULTS)) : ViewSettings
.getDefaultSettings();
}

public String getFunction() throws JSONException {
Expand All @@ -100,8 +104,11 @@ public String getDigest() {
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
md.update(toBytes(json.optString("analyzer")));
md.update(toBytes(json.optString("query_analyzer")));
md.update(toBytes(json.optString("defaults")));
md.update(toBytes(json.optString("similarity")));
md.update(toBytes(json.optString("index")));
md.update(toBytes(json.optString("configuration")));
return new BigInteger(1, md.digest()).toString(Character.MAX_RADIX);
} catch (final NoSuchAlgorithmException e) {
throw new Error("MD5 support missing.");
Expand Down
@@ -0,0 +1,7 @@
package com.github.rnewson.couchdb.lucene.util;

import org.json.JSONObject;

public interface Configurable {
void setConfiguration(JSONObject configuration);
}
Expand Up @@ -26,9 +26,7 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Fieldable;

import com.github.rnewson.couchdb.lucene.couchdb.Databasable;
import com.github.rnewson.couchdb.lucene.couchdb.Database;
import org.json.JSONObject;

/**
* This analyzer is used to facilitate scenarios where different fields require
Expand All @@ -52,7 +50,7 @@
* A PerFieldAnalyzer can be used like any other analyzer, for both indexing and
* query parsing.
*/
public final class PerFieldAnalyzer extends Analyzer implements Databasable {
public final class PerFieldAnalyzer extends Analyzer implements Configurable {
private final Analyzer defaultAnalyzer;
private final Map<String, Analyzer> analyzerMap = new HashMap<String, Analyzer>();

Expand Down Expand Up @@ -136,17 +134,17 @@ public int getOffsetGap(Fieldable field) {
return analyzer.getOffsetGap(field);
}

public void setDatabase(Database database) {
if (defaultAnalyzer instanceof Databasable) {
((Databasable) defaultAnalyzer).setDatabase(database);
public void setConfiguration(JSONObject configuration) {
if (defaultAnalyzer instanceof Configurable) {
((Configurable) defaultAnalyzer).setConfiguration(configuration);
}

Iterator<Map.Entry<String,Analyzer>> it = analyzerMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String,Analyzer> entry = it.next();
Analyzer analyzer = entry.getValue();
if (analyzer instanceof Databasable) {
((Databasable) analyzer).setDatabase(database);
if (analyzer instanceof Configurable) {
((Configurable) analyzer).setConfiguration(configuration);
}
}
}
Expand Down

0 comments on commit cd63a98

Please sign in to comment.