-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathStringSearch.java
More file actions
163 lines (128 loc) · 5.6 KB
/
StringSearch.java
File metadata and controls
163 lines (128 loc) · 5.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* 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.*;
import com.marklogic.client.admin.QueryOptionsManager;
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.SearchHandle;
import com.marklogic.client.io.StringHandle;
import com.marklogic.client.query.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* StringSearch illustrates searching for documents and iterating over results
* with string criteria referencing a constraint defined by options.
*/
public class StringSearch {
static final private String OPTIONS_NAME = "products";
static final private String[] filenames = {"curbappeal.xml", "flipper.xml", "justintime.xml"};
public static void main(String[] args)
throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException, ResourceNotResendableException
{
run(Util.loadProperties());
}
public static void run(ExampleProperties props)
throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException, ResourceNotResendableException
{
System.out.println("example: "+StringSearch.class.getName());
configure(props);
search(props);
tearDownExample(props);
}
public static void configure(ExampleProperties props)
throws FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException
{
DatabaseClient client = Util.newAdminClient(props);
// create a manager for writing query options
QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager();
// construct the query options
String options =
"<search:options "+
"xmlns:search='http://marklogic.com/appservices/search'>"+
"<search:constraint name='industry'>"+
"<search:value>"+
"<search:element name='industry' ns=''/>"+
"</search:value>"+
"</search:constraint>"+
"</search:options>";
// create a handle to send the query options
StringHandle writeHandle = new StringHandle(options);
// write the query options to the database
optionsMgr.writeOptions(OPTIONS_NAME, writeHandle);
// release the client
client.release();
}
public static void search(ExampleProperties props)
throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException
{
DatabaseClient client = Util.newClient(props);
setUpExample(client);
// create a manager for searching
QueryManager queryMgr = client.newQueryManager();
// create a search definition
StringQueryDefinition querydef = queryMgr.newStringDefinition(OPTIONS_NAME);
querydef.setCriteria("neighborhood industry:\"Real Estate\"");
// create a handle for the search results
SearchHandle resultsHandle = new SearchHandle();
// run the search
queryMgr.search(querydef, resultsHandle);
System.out.println("Matched "+resultsHandle.getTotalResults()+
" documents with '"+querydef.getCriteria()+"'\n");
// iterate over the result documents
MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults();
System.out.println("Listing "+docSummaries.length+" documents:\n");
for (MatchDocumentSummary docSummary: docSummaries) {
String uri = docSummary.getUri();
int score = docSummary.getScore();
// iterate over the match locations within a result document
MatchLocation[] locations = docSummary.getMatchLocations();
System.out.println("Matched "+locations.length+" locations in "+uri+" with "+score+" score:");
for (MatchLocation location: locations) {
// iterate over the snippets at a match location
for (MatchSnippet snippet : location.getSnippets()) {
boolean isHighlighted = snippet.isHighlighted();
if (isHighlighted)
System.out.print("[");
System.out.print(snippet.getText());
if (isHighlighted)
System.out.print("]");
}
System.out.println();
}
}
// release the client
client.release();
}
// set up by writing the document content and options used in the example query
public static void setUpExample(DatabaseClient client)
throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException
{
XMLDocumentManager docMgr = client.newXMLDocumentManager();
try ( InputStreamHandle contentHandle = new InputStreamHandle() ) {
for (String filename: filenames) {
InputStream docStream = Util.openStream("data"+File.separator+filename);
if (docStream == null)
throw new IOException("Could not read document example");
contentHandle.set(docStream);
docMgr.write("/example/"+filename, contentHandle);
}
}
}
// clean up by deleting the documents and query options used in the example query
public static void tearDownExample(ExampleProperties props)
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
{
DatabaseClient client = Util.newAdminClient(props);
XMLDocumentManager docMgr = client.newXMLDocumentManager();
for (String filename: filenames) {
docMgr.delete("/example/"+filename);
}
QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager();
optionsMgr.deleteOptions(OPTIONS_NAME);
client.release();
}
}