1
votes

I need to create a paginated search for json documents in one collection. Document structure:

{
   "Id": "OBJ-0000",
   "Title": "sample text",
   "Visible": true
}

I created element range indexes on fields Id, Title and Visible, and search options xml configuration:

<?xml  version="1.0" encoding="UTF-8"?>
<search:options xmlns:search="http://marklogic.com/appservices/search">
  <search:constraint name="id">
    <search:range facet="false" type="xs:string">
      <search:json-property>Id</search:json-property>
    </search:range>
  </search:constraint>
  <search:constraint name="title">
    <search:range facet="false" type="xs:string">
      <search:json-property>Title</search:json-property>
    </search:range>
  </search:constraint>
  <search:constraint name="visible">
    <search:value type="boolean">
      <search:json-property>Visible</search:json-property>
    </search:value>
  </search:constraint>
  <search:operator name="sort">
    <search:state name="idAsc">
      <search:sort-order direction="ascending">
        <search:json-property>Id</search:json-property>
      </search:sort-order>
    </search:state>
    <search:state name="idDesc">
      <search:sort-order direction="descending">
        <search:json-property>Id</search:json-property>
      </search:sort-order>
    </search:state>
    <search:state name="titleAsc">
      <search:sort-order direction="ascending">
        <search:json-property>Title</search:json-property>
      </search:sort-order>
      <search:sort-order direction="descending">
        <search:json-property>Id</search:json-property>
      </search:sort-order>
    </search:state>
    <search:state name="titleDesc">
      <search:sort-order direction="descending">
        <search:json-property>Title</search:json-property>
      </search:sort-order>
      <search:sort-order direction="descending">
        <search:json-property>Id</search:json-property>
      </search:sort-order>
    </search:state>
    <search:state name="isvisibleAsc">
      <search:sort-order direction="ascending">
        <search:json-property>Visible</search:json-property>
      </search:sort-order>
      <search:sort-order direction="descending">
        <search:json-property>Id</search:json-property>
      </search:sort-order>
    </search:state>
    <search:state name="isvisibleDesc">
      <search:sort-order direction="descending">
        <search:json-property>Visible</search:json-property>
      </search:sort-order>
      <search:sort-order direction="descending">
        <search:json-property>Id</search:json-property>
      </search:sort-order>
    </search:state>
  </search:operator>
  <search:return-results>true</search:return-results>
  <search:return-metrics>false</search:return-metrics>
  <search:transform-results apply="raw">
  </search:transform-results>
  <search:debug>false</search:debug>
</search:options>

After, I created POST request with reference to options file:

LATEST/search?format=json&pageLength=20&start=1&options=objectSearch   

with structured query:

 {
    "query": {
      "operator-state": {
        "operator-name": "sort",
        "state-name": "TitleAsc"
      },
      "and-query": {
        "term-query": { "text": "*exam*" }
      }
    }
}

and it works. But, however, when I make a query with the parameter start=1, in "total" I get more results than it really does, but if I specified start=20 for example, "total" is calculated correctly. This happens only when I use the search parameters with the wildcard (*) in the first place in the search term and term contains more than 3 characters. How I can fix this issue?

1
Did you enable wildcard options in the database settings? - grtjn
@grtjn yes, I turned on word searches, word positions, three character searches, three character word positions - Evgeny Malyuta
What about the trailing wildcard options? - grtjn

1 Answers

2
votes

From the documentation :

https://docs.marklogic.com/search:search

The output of search:search returns a element, which in turn contains a total attribute. The value of the total attribute is an estimate, based on the index resolution of the query, and it is not filtered for accuracy. The accuracy of the index resolution depends on the index configuration of the database, on the query, and on the data being searched.

This means that you will only receive an accurate total when all of the necessary indexes are turned on for your query.

Using the recommended Wildcard Index settings here should help you get accurate totals : https://docs.marklogic.com/guide/search-dev/wildcard#id_14163

From your comment above, you appear to be missing a word lexicon in the codepoint collation.