MarkLogic - 使用 PojoQueryBuilder 进行 'like' 搜索

MarkLogic - Use PojoQueryBuilder to do a 'like' search

假设我有一个 JSON 在 ML8 中保存为 :

{
  "com.marklogic.poc.java.api.pojos.ProviderJSON": {
    "providerId": "1111",
    "name": "John Doe",
    "age": "41",
    "gender": "M"
  }
}

我尝试使用以下方法进行搜索以搜索整个 json 或任何特定属性:

但是如果我想查找 'Joh' 是否出现在名称 属性 中,我不能使用以上任何一个。有没有可以使用的api方法..

Goel,你可以用一个word search with wildcard. You'll need to turn on one of the wildcarding settings for your database. Below is a simple QBE例子。我在我的数据库上启用了 "trailing wildcard searches"。

    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();

我转到 http://localhost:8001,选择 "Documents"(数据库将 OOTB 与端口 8000 相关联)并将 "three character searches" 更改为 "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(); }
    }
}

请注意,值查询需要第二个通配符来匹配值中的任何其他词。

希望对您有所帮助。