当前位置: 首页 > elasticsearch, 搜索 > 正文

elasticsearch addScriptField函数实例

1 星2 星3 星4 星5 星 (3 次投票, 评分: 3.67, 总分: 5)
Loading ... Loading ...
baidu_share

现在,我们来学习一下addScriptField函数的使用。该函数一般适合numeric field 的匹配
直接上代码吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
	public void test2() throws Exception{
		Client client = ESServiceSingleton2.getTransportClient();
		client.admin().indices().prepareDelete().execute().actionGet();
        client.admin().indices().prepareCreate("test").execute().actionGet();
        client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();
 
        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
                .startObject("num1").field("type", "double").field("store", "yes").endObject()
                .endObject().endObject().endObject().string();
 
        client.admin().indices().preparePutMapping().setType("type1").setSource(mapping).execute().actionGet();
 
        client.prepareIndex("test", "type1", "1")
                .setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).field("date", "2013-01-01T00:00:00").endObject())
                .execute().actionGet();
        client.admin().indices().prepareFlush().execute().actionGet();
        client.prepareIndex("test", "type1", "2")
                .setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 2.0f).field("date", "2013-01-01T00:00:25").endObject())
                .execute().actionGet();
        client.admin().indices().prepareFlush().execute().actionGet();
        client.prepareIndex("test", "type1", "3")
                .setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 3.0f).field("date", "203-01-01T00:02:00").endObject())
                .execute().actionGet();
        client.admin().indices().refresh(new RefreshRequest("test")).actionGet();
 
 
        SearchResponse response = client.prepareSearch()
                .setQuery(QueryBuilders.matchAllQuery())
                .addSort("num1", SortOrder.ASC)//num1升序
                .addScriptField("sNum1", "doc['num1'].value")//num1的值作为新的一个sNum1 field,该field不会     
     //被存储
 
                .addScriptField("sNum1_field", "_fields['num1'].value")//field 中num1 域的值作为新的一个
 
//sNum1_field,该 field不会被存储
                .addScriptField("date1", "doc['date'].date.millis")
                .execute().actionGet();
 
        System.out.println("Failures " + Arrays.toString(response.getShardFailures())+" " +response.getShardFailures().length);
 
        System.out.println(response.getHits().totalHits());
        System.out.println(response.getHits().getAt(0).isSourceEmpty());
        System.out.println(response.getHits().getAt(0).id());
        System.out.println( response.getHits().getAt(0).fields().get("sNum1").values().get(0));
        System.out.println( response.getHits().getAt(0).fields().get("sNum1_field").values().get(0));
        System.out.println( response.getHits().getAt(0).fields().get("date1").values().get(0));
        System.out.println(response.getHits().getAt(1).id());
        System.out.println(response.getHits().getAt(1).fields().get("sNum1").values().get(0));
        System.out.println( response.getHits().getAt(1).fields().get("sNum1_field").values().get(0));
        System.out.println( response.getHits().getAt(1).fields().get("date1").values().get(0));
        System.out.println(response.getHits().getAt(2).id());
        System.out.println(response.getHits().getAt(2).fields().get("sNum1").values().get(0));
        System.out.println( response.getHits().getAt(2).fields().get("sNum1_field").values().get(0));
        System.out.println( response.getHits().getAt(2).fields().get("date1").values().get(0));
 
 
        Map<String, Object> params = MapBuilder.<String, Object>newMapBuilder().put("factor", 2.0).map();
        response = client.prepareSearch()
                .setQuery(QueryBuilders.matchAllQuery())
                .addSort("num1", SortOrder.ASC)
                .addScriptField("sNum1", "doc['num1'].value * factor", params)
                .execute().actionGet();
 
        System.out.println(response.getHits().totalHits());
        System.out.println(response.getHits().getAt(0).id());
        System.out.println((Double) response.getHits().getAt(0).fields().get("sNum1").values().get(0));
        System.out.println(response.getHits().getAt(1).id());
        System.out.println((Double) response.getHits().getAt(1).fields().get("sNum1").values().get(0));
        System.out.println(response.getHits().getAt(2).id());
        System.out.println(response.getHits().getAt(2).fields().get("sNum1").values().get(0));
	}

获取 client 请参考elasticsearch获取java client实例

本文固定链接: http://www.chepoo.com/elasticsearch-add-script-field-demo.html | IT技术精华网

elasticsearch addScriptField函数实例:等您坐沙发呢!

发表评论