MarkLogic—使用PojoQueryBuilder执行';比如';搜索
假设我有一个JSON保存在ML8中为:MarkLogic—使用PojoQueryBuilder执行';比如';搜索,marklogic,nosql,Marklogic,Nosql,假设我有一个JSON保存在ML8中为: { "com.marklogic.poc.java.api.pojos.ProviderJSON": { "providerId": "1111", "name": "John Doe", "age": "41", "gender": "M" } } 我尝试使用以下方法搜索整个json或任何特定属性: 术语 话 价值观 但如果我想找出name属性中是否存在“Joh”,我不能使用上面的任何一个。是否有可用的api
{
"com.marklogic.poc.java.api.pojos.ProviderJSON": {
"providerId": "1111",
"name": "John Doe",
"age": "41",
"gender": "M"
}
}
我尝试使用以下方法搜索整个json或任何特定属性:
- 术语
- 话
- 价值观
DatabaseClient client =
DatabaseClientFactory.newClient(
"localhost",
8000,
"admin",
"admin",
Authentication.DIGEST);
QueryManager queryMgr = client.newQueryManager();
StringHandle rawHandle = new StringHandle();
String rawJSONQuery =
"{ \"$query\": { \"name\": { \"$word\": \"Joh*\" } }}";
rawHandle.withFormat(Format.JSON).set(rawJSONQuery);
RawQueryByExampleDefinition querydef =
queryMgr.newRawQueryByExampleDefinition(rawHandle);
SearchHandle resultsHandle =
queryMgr.search(querydef, new SearchHandle());
client.release();
我转到,选择了“文档”(与端口8000关联的OOTB数据库),并将“三字符搜索”更改为“true”。那么下面的代码对我适用:
test/ProviderJSON.java:
package test;
import com.marklogic.client.pojo.annotation.Id;
public class ProviderJSON {
@Id
public String providerId;
public String name;
public String age;
public String gender;
public String toString() {
return
"providerId:" + providerId + "\n" +
"name:" + name + "\n" +
"age:" + age + "\n" +
"gender:" + gender + "\n";
}
}
和Test.java:
import test.ProviderJSON;
import com.marklogic.client.DatabaseClientFactory;
import static com.marklogic.client.DatabaseClientFactory.Authentication.DIGEST;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.pojo.PojoRepository;
import com.marklogic.client.pojo.PojoPage;
import com.marklogic.client.pojo.PojoQueryDefinition;
public class Test {
public static void main(String[] args) {
DatabaseClient client =
DatabaseClientFactory.newClient("localhost", 8000, "admin", "admin", DIGEST);
PojoRepository<ProviderJSON, String> providers =
client.newPojoRepository(ProviderJSON.class, String.class);
ProviderJSON provider1 = new ProviderJSON();
provider1.providerId = "1111";
provider1.name = "John Doe";
provider1.age = "41";
provider1.gender = "M";
providers.write(provider1);
PojoQueryDefinition query = client.newQueryManager()
.newStringDefinition().withCriteria("Joh*");
int start = 1;
PojoPage<ProviderJSON> matches = providers.search(query, start);
try {
System.out.println("string matches.size() =[" + matches.size() + "]");
for ( ProviderJSON match : matches ) {
System.out.println("string match.providerId =[" + match.providerId + "]");
}
} finally { matches.close(); }
query = providers.getQueryBuilder()
.word("name", "Joh*");
matches = providers.search(query, start);
try {
System.out.println("word matches.size() =[" + matches.size() + "]");
for ( ProviderJSON match : matches ) {
System.out.println("word match.providerId =[" + match.providerId + "]");
}
} finally { matches.close(); }
query = providers.getQueryBuilder()
.value("name", "Joh* *");
matches = providers.search(query, start);
try {
System.out.println("value matches.size() =[" + matches.size() + "]");
for ( ProviderJSON match : matches ) {
System.out.println("value match.providerId =[" + match.providerId + "]");
}
} finally { matches.close(); }
}
}
import test.ProviderJSON;
导入com.marklogic.client.DatabaseClientFactory;
导入静态com.marklogic.client.DatabaseClientFactory.Authentication.DIGEST;
导入com.marklogic.client.DatabaseClient;
导入com.marklogic.client.pojo.PojoRepository;
导入com.marklogic.client.pojo.PojoPage;
导入com.marklogic.client.pojo.PojoQueryDefinition;
公开课考试{
公共静态void main(字符串[]args){
数据库客户端=
DatabaseClientFactory.newClient(“localhost”,8000,“admin”,“admin”,DIGEST);
PojoRepository提供者=
newPojoRepository(ProviderJSON.class,String.class);
ProviderJSON provider1=新ProviderJSON();
provider1.providerId=“1111”;
provider1.name=“John Doe”;
provider1.age=“41”;
provider1.gender=“M”;
providers.write(provider1);
PojoQueryDefinition query=client.newQueryManager()
.newStringDefinition()。带有条件(“Joh*”);
int start=1;
PojoPage matches=providers.search(查询,开始);
试一试{
System.out.println(“字符串匹配.size()=[”+匹配.size()+“]);
for(ProviderJSON匹配:匹配){
System.out.println(“字符串match.providerId=[“+match.providerId+”]);
}
}最后{matches.close();}
query=providers.getQueryBuilder()
.单词(“名称”、“Joh*”);
匹配=提供者。搜索(查询,开始);
试一试{
System.out.println(“word matches.size()=[”+matches.size()+“]);
for(ProviderJSON匹配:匹配){
System.out.println(“word match.providerId=[“+match.providerId+”]);
}
}最后{matches.close();}
query=providers.getQueryBuilder()
.价值(“名称”、“Joh**”);
匹配=提供者。搜索(查询,开始);
试一试{
System.out.println(“值匹配.size()=[”+匹配.size()+“]);
for(ProviderJSON匹配:匹配){
System.out.println(“value match.providerId=[“+match.providerId+”]);
}
}最后{matches.close();}
}
}
请注意,值查询需要第二个通配符来匹配值中的任何其他单词
我希望这会有帮助。我会试试这个,戴夫。。在此之前,我只是想问一下PojoQueryBuilder有什么方法,这样我就可以得到java对象了。看起来Sam已经回答了你的问题。谢谢Sam。。这三种方法都在起作用。看到这三种方法只是想知道,对于大量的文件,哪一种性能最好,或者这三种方法都差不多!除非你的数据集庞大或者你的流量令人难以置信,否则我相信这三者之间的速度差异在统计学上是微不足道的。如果您不关心要匹配的字段,那么第一种方法(word查询)可能是最简单的。第二个(元素词查询)和第三个(元素值查询)需要一个额外的查询解析步骤来过滤特定元素中的匹配项。在优化查询方面有很多优秀的资源: