求助elasticsearch查询没有数据返回
代码如下
package paoding;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.search.SearchHit;
public class MyTest
{
Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress(
"192.168.10.200", 9300));
public void createIndex(String indexName, String indexType)
throws Exception
{
try
{
try
{
// 预定义一个索引
client.admin().indices().prepareCreate(indexName).execute()
.actionGet();
// 定义索引字段属性
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject();
mapping = mapping.startObject(indexType);
mapping = mapping.startObject("properties");
mapping = mapping.startObject("id").field("type", "string")
.field("store", "yes").endObject();
mapping = mapping.startObject("title").field("type", "string")
.field("indexAnalyzer", "ik")
.field("searchAnalyzer", "ik").field("store", "yes")
.endObject();
mapping = mapping.startObject("createTime")
.field("type", "string")
.field("indexAnalyzer", "ik")
.field("searchAnalyzer", "ik").field("store", "yes")
.endObject();
mapping = mapping.endObject();
mapping = mapping.endObject();
mapping = mapping.endObject();
PutMappingRequest mappingRequest = Requests
.putMappingRequest(indexName).type(indexType)
.source(mapping);
client.admin().indices().putMapping(mappingRequest).actionGet();
}
catch (IndexAlreadyExistsException e)
{
System.out.println("索引库已存在");
}
IndexRequestBuilder requestBuilder = client.prepareIndex(
indexName, indexType).setRefresh(true);
for (int i = 0; i < 10; ++i)
{
String id = UUID.randomUUID().toString();
// 生成文档
XContentBuilder doc = XContentFactory.jsonBuilder()
.startObject();
doc = doc.startObject("article");
doc = doc.field("id", id);
doc = doc.field("title", "java附魔1111111大师2222222222");
doc = doc.field("createTime", new Date());
doc = doc.endObject();
doc = doc.endObject();
// 创建索引
requestBuilder.setSource(doc).execute().actionGet();
}
}
catch (Exception e)
{
e.printStackTrace();
}
client.close();
}
public void search(String indexName, String indexType) throws Exception
{
try
{
client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress(
"192.168.10.200", 9300));
// QueryBuilder qb = QueryBuilders.fieldQuery("title", "大师");
QueryBuilder qb = QueryBuilders.filteredQuery(
QueryBuilders.termQuery("title", "大师"), null);
SearchResponse scrollResp = client.prepareSearch(indexName)
.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000)).setQuery(qb).setSize(100)
.execute().actionGet();
while (true)
{
scrollResp = client
.prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(600000)).execute().actionGet();
if (scrollResp.getHits().hits().length == 0)
{
System.out.println("==============");
break;
}
for (SearchHit hit : scrollResp.getHits())
{
Map<String, Object> source = hit.getSource();
if (!source.isEmpty())
{
for (Iterator<Entry<String, Object>> it = source
.entrySet().iterator(); it.hasNext();)
{
Entry<String, Object> entry = it.next();
System.out.println(entry.getKey() + "======="
+ entry.getValue());
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
client.close();
}
}
public static void main(String[] args)
{
MyTest test = new MyTest();
try
{
test.createIndex("appww", "apptypeww");
test.search("appww", "apptypeww");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
希望有时间的可以看下是哪里有问题,找了一天不知道哪里有问题