Make AnalyzeRequest support field() and type()

This commit is contained in:
melix 2011-07-12 17:20:58 +02:00 committed by kimchy
parent b627ba06c6
commit 8564c20378
5 changed files with 123 additions and 4 deletions

View File

@ -42,6 +42,10 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
private String analyzer; private String analyzer;
private String type;
private String field;
AnalyzeRequest() { AnalyzeRequest() {
} }
@ -79,6 +83,24 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
return this.analyzer; return this.analyzer;
} }
public AnalyzeRequest type(String type) {
this.type = type;
return this;
}
public String type() {
return this.type;
}
public AnalyzeRequest field(String field) {
this.field = field;
return this;
}
public String field() {
return this.field;
}
/** /**
* if this operation hits a node with a local relevant shard, should it be preferred * if this operation hits a node with a local relevant shard, should it be preferred
* to be executed on, or just do plain round robin. Defaults to <tt>true</tt> * to be executed on, or just do plain round robin. Defaults to <tt>true</tt>
@ -106,17 +128,29 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
if (in.readBoolean()) { if (in.readBoolean()) {
analyzer = in.readUTF(); analyzer = in.readUTF();
} }
if (in.readBoolean()) {
type = in.readUTF();
}
if (in.readBoolean()) {
field = in.readUTF();
}
} }
@Override public void writeTo(StreamOutput out) throws IOException { @Override public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeUTF(index); out.writeUTF(index);
out.writeUTF(text); out.writeUTF(text);
if (analyzer == null) { writeOption(out, analyzer);
writeOption(out, type);
writeOption(out, field);
}
private void writeOption(StreamOutput out, String value) throws IOException {
if (value==null) {
out.writeBoolean(false); out.writeBoolean(false);
} else { } else {
out.writeBoolean(true); out.writeBoolean(true);
out.writeUTF(analyzer); out.writeUTF(value);
} }
} }
} }

View File

@ -36,6 +36,7 @@ import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.io.FastStringReader;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.service.IndexService; import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
@ -86,9 +87,18 @@ public class TransportAnalyzeAction extends TransportSingleCustomOperationAction
IndexService indexService = indicesService.indexServiceSafe(request.index()); IndexService indexService = indicesService.indexServiceSafe(request.index());
Analyzer analyzer = null; Analyzer analyzer = null;
String field = "contents"; String field = "contents";
if (request.analyzer() != null) { String dtype = null;
if (request.field()!=null) field = request.field();
if (request.type()!=null) dtype = request.type();
if (request.field()!=null || request.type()!=null) {
final DocumentMapper mapper = indexService.mapperService().documentMapper(dtype);
if (mapper!=null) {
analyzer = mapper.mappers().indexAnalyzer();
}
}
if (analyzer==null && request.analyzer() != null) {
analyzer = indexService.analysisService().analyzer(request.analyzer()); analyzer = indexService.analysisService().analyzer(request.analyzer());
} else { } else if (analyzer==null) {
analyzer = indexService.analysisService().defaultIndexAnalyzer(); analyzer = indexService.analysisService().defaultIndexAnalyzer();
} }
if (analyzer == null) { if (analyzer == null) {

View File

@ -56,4 +56,14 @@ public class AnalyzeRequestBuilder extends BaseIndicesRequestBuilder<AnalyzeRequ
@Override protected void doExecute(ActionListener<AnalyzeResponse> listener) { @Override protected void doExecute(ActionListener<AnalyzeResponse> listener) {
client.analyze(request, listener); client.analyze(request, listener);
} }
public AnalyzeRequestBuilder field(String field) {
request.field(field);
return this;
}
public AnalyzeRequestBuilder type(String type) {
request.type(type);
return this;
}
} }

View File

@ -63,6 +63,8 @@ public class RestAnalyzeAction extends BaseRestHandler {
AnalyzeRequest analyzeRequest = new AnalyzeRequest(request.param("index"), text); AnalyzeRequest analyzeRequest = new AnalyzeRequest(request.param("index"), text);
analyzeRequest.preferLocal(request.paramAsBoolean("prefer_local", analyzeRequest.preferLocalShard())); analyzeRequest.preferLocal(request.paramAsBoolean("prefer_local", analyzeRequest.preferLocalShard()));
analyzeRequest.analyzer(request.param("analyzer")); analyzeRequest.analyzer(request.param("analyzer"));
analyzeRequest.type(request.param("type"));
analyzeRequest.field(request.param("field"));
client.admin().indices().analyze(analyzeRequest, new ActionListener<AnalyzeResponse>() { client.admin().indices().analyze(analyzeRequest, new ActionListener<AnalyzeResponse>() {
@Override public void onResponse(AnalyzeResponse response) { @Override public void onResponse(AnalyzeResponse response) {
try { try {

View File

@ -20,7 +20,9 @@
package org.elasticsearch.test.integration.indices.analyze; package org.elasticsearch.test.integration.indices.analyze;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -70,4 +72,65 @@ public class AnalyzeActionTests extends AbstractNodesTests {
assertThat(token.endOffset(), equalTo(14)); assertThat(token.endOffset(), equalTo(14));
} }
} }
@Test public void analyzerWithFieldOrTypeTests() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test").execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
client.admin().indices().preparePutMapping("test")
.setType("document").setSource(
"{\n" +
" \"document\":{\n" +
" \"properties\":{\n" +
" \"simple\":{\n" +
" \"type\":\"string\",\n" +
" \"analyzer\": \"simple\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}"
).execute().actionGet();
for (int i = 0; i < 10; i++) {
final AnalyzeRequestBuilder requestBuilder = client.admin().indices().prepareAnalyze("test", "THIS IS A TEST");
requestBuilder.type("document");
requestBuilder.field("simple");
AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet();
assertThat(analyzeResponse.tokens().size(), equalTo(4));
AnalyzeResponse.AnalyzeToken token = analyzeResponse.tokens().get(3);
assertThat(token.term(), equalTo("test"));
assertThat(token.startOffset(), equalTo(10));
assertThat(token.endOffset(), equalTo(14));
}
// test that using the document type only uses the default analyzer
for (int i = 0; i < 10; i++) {
final AnalyzeRequestBuilder requestBuilder = client.admin().indices().prepareAnalyze("test", "THIS IS A TEST");
requestBuilder.type("document");
AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet();
assertThat(analyzeResponse.tokens().size(), equalTo(1));
AnalyzeResponse.AnalyzeToken token = analyzeResponse.tokens().get(0);
assertThat(token.term(), equalTo("test"));
assertThat(token.startOffset(), equalTo(10));
assertThat(token.endOffset(), equalTo(14));
}
// test that using the field name only uses the default analyzer
for (int i = 0; i < 10; i++) {
final AnalyzeRequestBuilder requestBuilder = client.admin().indices().prepareAnalyze("test", "THIS IS A TEST");
requestBuilder.field("simple");
AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet();
assertThat(analyzeResponse.tokens().size(), equalTo(1));
AnalyzeResponse.AnalyzeToken token = analyzeResponse.tokens().get(0);
assertThat(token.term(), equalTo("test"));
assertThat(token.startOffset(), equalTo(10));
assertThat(token.endOffset(), equalTo(14));
}
}
} }