-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathRawClientAlert.java
More file actions
141 lines (114 loc) · 4.76 KB
/
RawClientAlert.java
File metadata and controls
141 lines (114 loc) · 4.76 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.example.cookbook;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.alerting.RuleDefinition;
import com.marklogic.client.alerting.RuleDefinitionList;
import com.marklogic.client.alerting.RuleManager;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.example.cookbook.Util.ExampleProperties;
import com.marklogic.client.io.InputStreamHandle;
import com.marklogic.client.io.StringHandle;
import com.marklogic.client.query.QueryManager;
import com.marklogic.client.query.StringQueryDefinition;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
/**
* RawClientAlert illustrates defining and finding rules that match documents.
*/
public class RawClientAlert {
static final private String RULE_NAME = "real-estate";
static final private String[] filenames = {"curbappeal.xml", "flipper.xml", "justintime.xml"};
public static void main(String[] args) throws IOException {
run(Util.loadProperties());
}
public static void run(ExampleProperties props) throws IOException {
System.out.println("example: "+RawClientAlert.class.getName());
DatabaseClient client = Util.newClient(props);
setUpExample(client);
configure(client);
match(client);
tearDownExample(client);
// release the client
client.release();
}
// set up alert rules
public static void configure(DatabaseClient client) throws IOException {
// create a manager for configuring rules
RuleManager ruleMgr = client.newRuleManager();
// specify a rule in raw XML (raw JSON is also supported
// as well as a POJO rule definition)
String rawRule =
"<rapi:rule xmlns:rapi='http://marklogic.com/rest-api'>"+
"<rapi:name>"+RULE_NAME+"</rapi:name>"+
"<rapi:description>industry of Real Estate</rapi:description>"+
"<search:search "+
"xmlns:search='http://marklogic.com/appservices/search'>"+
"<search:query>"+
"<search:value-constraint-query>"+
"<search:constraint-name>industry</search:constraint-name>"+
"<search:text>Real Estate</search:text>"+
"</search:value-constraint-query>"+
"</search:query>"+
"<search:options>"+
"<search:constraint name='industry'>"+
"<search:value>"+
"<search:element name='industry' ns=''/>"+
"</search:value>"+
"</search:constraint>"+
"</search:options>"+
"</search:search>"+
"<rapi:rule-metadata>"+
"<correlate-with>/demographic-statistics?zipcode=</correlate-with>"+
"</rapi:rule-metadata>"+
"</rapi:rule>";
// create a handle for writing the rule
StringHandle writeHandle = new StringHandle(rawRule);
// write the rule to the database
ruleMgr.writeRule(RULE_NAME, writeHandle);
}
// match documents against the alert rules
public static void match(DatabaseClient client) throws IOException {
// create a manager for document search criteria
QueryManager queryMgr = client.newQueryManager();
// specify the search criteria for the documents
String criteria = "neighborhoods";
StringQueryDefinition querydef = queryMgr.newStringDefinition();
querydef.setCriteria(criteria);
// create a manager for matching rules
RuleManager ruleMgr = client.newRuleManager();
// match the rules against the documents qualified by the criteria
RuleDefinitionList matchedRules = ruleMgr.match(querydef, new RuleDefinitionList());
// iterate over the matched rules
Iterator<RuleDefinition> ruleItr = matchedRules.iterator();
while (ruleItr.hasNext()) {
RuleDefinition rule = ruleItr.next();
System.out.println(
"document criteria "+criteria+" matched rule "+
rule.getName()+" with metadata "+rule.getMetadata()
);
}
}
// set up by writing the document content used in the example query
public static void setUpExample(DatabaseClient client) throws IOException {
XMLDocumentManager docMgr = client.newXMLDocumentManager();
for (String filename: filenames) {
try ( InputStream docStream = Util.openStream("data"+File.separator+filename) ) {
docMgr.write("/example/"+filename, new InputStreamHandle(docStream));
}
}
}
// clean up by deleting the documents used in the example query and
// the rules used for matching
public static void tearDownExample(DatabaseClient client) {
XMLDocumentManager docMgr = client.newXMLDocumentManager();
for (String filename: filenames) {
docMgr.delete("/example/"+filename);
}
RuleManager ruleMgr = client.newRuleManager();
ruleMgr.delete(RULE_NAME);
}
}