make analyzer request just use field, and allow it to have type.field_name option (or just field name)

This commit is contained in:
kimchy 2011-07-13 03:15:36 +03:00
parent 8564c20378
commit cfc45b0ab9
5 changed files with 31 additions and 72 deletions

View File

@ -42,8 +42,6 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
private String analyzer;
private String type;
private String field;
AnalyzeRequest() {
@ -83,15 +81,6 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
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;
@ -128,9 +117,6 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
if (in.readBoolean()) {
analyzer = in.readUTF();
}
if (in.readBoolean()) {
type = in.readUTF();
}
if (in.readBoolean()) {
field = in.readUTF();
}
@ -141,12 +127,11 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
out.writeUTF(index);
out.writeUTF(text);
writeOption(out, analyzer);
writeOption(out, type);
writeOption(out, field);
}
private void writeOption(StreamOutput out, String value) throws IOException {
if (value==null) {
if (value == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);

View File

@ -36,7 +36,7 @@ import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.FastStringReader;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.threadpool.ThreadPool;
@ -86,19 +86,20 @@ public class TransportAnalyzeAction extends TransportSingleCustomOperationAction
@Override protected AnalyzeResponse shardOperation(AnalyzeRequest request, int shardId) throws ElasticSearchException {
IndexService indexService = indicesService.indexServiceSafe(request.index());
Analyzer analyzer = null;
String field = "contents";
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();
String field = null;
if (request.field() != null) {
FieldMapper fieldMapper = indexService.mapperService().smartNameFieldMapper(request.field());
if (fieldMapper != null) {
analyzer = fieldMapper.indexAnalyzer();
field = fieldMapper.names().indexName();
}
}
if (analyzer==null && request.analyzer() != null) {
if (field == null) {
field = "_all";
}
if (analyzer == null && request.analyzer() != null) {
analyzer = indexService.analysisService().analyzer(request.analyzer());
} else if (analyzer==null) {
} else if (analyzer == null) {
analyzer = indexService.analysisService().defaultIndexAnalyzer();
}
if (analyzer == null) {

View File

@ -61,9 +61,4 @@ public class AnalyzeRequestBuilder extends BaseIndicesRequestBuilder<AnalyzeRequ
request.field(field);
return this;
}
public AnalyzeRequestBuilder type(String type) {
request.type(type);
return this;
}
}

View File

@ -27,7 +27,12 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.XContentRestResponse;
import org.elasticsearch.rest.XContentThrowableRestResponse;
import java.io.IOException;
@ -63,7 +68,6 @@ public class RestAnalyzeAction extends BaseRestHandler {
AnalyzeRequest analyzeRequest = new AnalyzeRequest(request.param("index"), text);
analyzeRequest.preferLocal(request.paramAsBoolean("prefer_local", analyzeRequest.preferLocalShard()));
analyzeRequest.analyzer(request.param("analyzer"));
analyzeRequest.type(request.param("type"));
analyzeRequest.field(request.param("field"));
client.admin().indices().analyze(analyzeRequest, new ActionListener<AnalyzeResponse>() {
@Override public void onResponse(AnalyzeResponse response) {

View File

@ -20,7 +20,6 @@
package org.elasticsearch.test.integration.indices.analyze;
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.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests;
@ -85,22 +84,21 @@ public class AnalyzeActionTests extends AbstractNodesTests {
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();
"{\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");
requestBuilder.field("document.simple");
AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet();
assertThat(analyzeResponse.tokens().size(), equalTo(4));
AnalyzeResponse.AnalyzeToken token = analyzeResponse.tokens().get(3);
@ -108,29 +106,5 @@ public class AnalyzeActionTests extends AbstractNodesTests {
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));
}
}
}