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 analyzer;
private String type;
private String field; private String field;
AnalyzeRequest() { AnalyzeRequest() {
@ -83,15 +81,6 @@ 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) { public AnalyzeRequest field(String field) {
this.field = field; this.field = field;
return this; return this;
@ -128,9 +117,6 @@ 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()) { if (in.readBoolean()) {
field = in.readUTF(); field = in.readUTF();
} }
@ -141,12 +127,11 @@ public class AnalyzeRequest extends SingleCustomOperationRequest {
out.writeUTF(index); out.writeUTF(index);
out.writeUTF(text); out.writeUTF(text);
writeOption(out, analyzer); writeOption(out, analyzer);
writeOption(out, type);
writeOption(out, field); writeOption(out, field);
} }
private void writeOption(StreamOutput out, String value) throws IOException { private void writeOption(StreamOutput out, String value) throws IOException {
if (value==null) { if (value == null) {
out.writeBoolean(false); out.writeBoolean(false);
} else { } else {
out.writeBoolean(true); 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.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.mapper.FieldMapper;
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,19 +86,20 @@ public class TransportAnalyzeAction extends TransportSingleCustomOperationAction
@Override protected AnalyzeResponse shardOperation(AnalyzeRequest request, int shardId) throws ElasticSearchException { @Override protected AnalyzeResponse shardOperation(AnalyzeRequest request, int shardId) throws ElasticSearchException {
IndexService indexService = indicesService.indexServiceSafe(request.index()); IndexService indexService = indicesService.indexServiceSafe(request.index());
Analyzer analyzer = null; Analyzer analyzer = null;
String field = "contents"; String field = null;
String dtype = null; if (request.field() != null) {
if (request.field()!=null) field = request.field(); FieldMapper fieldMapper = indexService.mapperService().smartNameFieldMapper(request.field());
if (request.type()!=null) dtype = request.type(); if (fieldMapper != null) {
if (request.field()!=null || request.type()!=null) { analyzer = fieldMapper.indexAnalyzer();
final DocumentMapper mapper = indexService.mapperService().documentMapper(dtype); field = fieldMapper.names().indexName();
if (mapper!=null) {
analyzer = mapper.mappers().indexAnalyzer();
} }
} }
if (analyzer==null && request.analyzer() != null) { if (field == null) {
field = "_all";
}
if (analyzer == null && request.analyzer() != null) {
analyzer = indexService.analysisService().analyzer(request.analyzer()); analyzer = indexService.analysisService().analyzer(request.analyzer());
} else if (analyzer==null) { } else if (analyzer == null) {
analyzer = indexService.analysisService().defaultIndexAnalyzer(); analyzer = indexService.analysisService().defaultIndexAnalyzer();
} }
if (analyzer == null) { if (analyzer == null) {

View File

@ -61,9 +61,4 @@ public class AnalyzeRequestBuilder extends BaseIndicesRequestBuilder<AnalyzeRequ
request.field(field); request.field(field);
return this; 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.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; 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; import java.io.IOException;
@ -63,7 +68,6 @@ 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")); 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) {

View File

@ -20,7 +20,6 @@
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.client.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
@ -85,22 +84,21 @@ public class AnalyzeActionTests extends AbstractNodesTests {
client.admin().indices().preparePutMapping("test") client.admin().indices().preparePutMapping("test")
.setType("document").setSource( .setType("document").setSource(
"{\n" + "{\n" +
" \"document\":{\n" + " \"document\":{\n" +
" \"properties\":{\n" + " \"properties\":{\n" +
" \"simple\":{\n" + " \"simple\":{\n" +
" \"type\":\"string\",\n" + " \"type\":\"string\",\n" +
" \"analyzer\": \"simple\"\n" + " \"analyzer\": \"simple\"\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +
"}" "}"
).execute().actionGet(); ).execute().actionGet();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
final AnalyzeRequestBuilder requestBuilder = client.admin().indices().prepareAnalyze("test", "THIS IS A TEST"); final AnalyzeRequestBuilder requestBuilder = client.admin().indices().prepareAnalyze("test", "THIS IS A TEST");
requestBuilder.type("document"); requestBuilder.field("document.simple");
requestBuilder.field("simple");
AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet(); AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet();
assertThat(analyzeResponse.tokens().size(), equalTo(4)); assertThat(analyzeResponse.tokens().size(), equalTo(4));
AnalyzeResponse.AnalyzeToken token = analyzeResponse.tokens().get(3); AnalyzeResponse.AnalyzeToken token = analyzeResponse.tokens().get(3);
@ -108,29 +106,5 @@ public class AnalyzeActionTests extends AbstractNodesTests {
assertThat(token.startOffset(), equalTo(10)); assertThat(token.startOffset(), equalTo(10));
assertThat(token.endOffset(), equalTo(14)); 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));
}
} }
} }