So there's one 'language' element per document, and many documents? I would 
create one path range index per language: maybe '//language/value[@xml:lang eq 
"en"]' and '//language/value[@xml:lang eq "zh"]'. But if possible specify the 
full path: something like '/a/b/language' rather than '//language'.

https://docs.marklogic.com/guide/admin/range_index

The reason to create two separate path range indexes is that each range index 
entry will contain a value and a document id. When you sort, the 'order by' 
clause can only use one value per document. So it's best if you only have one 
value per document in the range index you're using, and the best way to arrange 
that is to have a separate index for each language.

Once you have the range indexes in place, you can use 
https://docs.marklogic.com/xdmp:query-trace to double-check that they will be 
used by your code.

-- Mike

On 13 Jun 2014, at 00:59 , Ashish Bhardwaj <[email protected]> wrote:

> Hello Mike,
> 
> Thanks a lot for your reply.
> 
> I am storing data in Marklogic using the xml I mentioned before. Here is an 
> simplified version of xml that I am storing in ML:
> <language>
>     <value xml:lang="en">English value-1</value>
>     <value xml:lang="zh">chines word-1</value>
> </language>
> 
> I want to sort in either "en" or "zh" language. I think the below statement 
> would work for me:
> let $lang := (: language to be used in sorting :)
> 
> for $i in (: some cts to get data from ML :)
> order by  $i/value[@xml:lang eq $lang]
> 
> Please suggest how to go with its indexing. Which index is most prominent in 
> order to sort in both language.
> 
> Sorry for my ignorance if I am lacking in some most basic information.
> 
> Regards,
> Ashish
> 
> 
> On Wed, Jun 11, 2014 at 10:04 PM, Michael Blakeley <[email protected]> wrote:
> It's not clear what it means to "sort the above xml based on the value 
> element for xml:lang="zh" or xml:lang="en" depending on some inputs". The 
> structure includes multiple 'language' element, each with multiple 
> language-annotated 'value' elements. Do you want to sort the 'language' 
> elements by 'value' and language?
> 
> If so, range indexes won't help because this will be an in-memory sort 
> anyway. Range indexes are only used for very specific sorts, when the results 
> are coming from the database and are aligned with the range index. See 
> https://docs.marklogic.com/guide/performance/order_by for details.
> 
> If we're sorting in memory anyway there's no reason to set up a range index. 
> The next hurdle is discussed in 
> http://stackoverflow.com/questions/23769228/dynamic-sort-order-by-based-on-variable-in-xquery
>  - the dynamic sort problem. Basically XQuery doesn't have a nice idiom for 
> specifying dynamic sort keys. So you'll end up either building a string and 
> calling xdmp:value, or enumerating every possible sort key. Neither option is 
> ideal, but they both work.
> 
> Here's an example of how enumeration might work. First define the language 
> elements as $list and set the value of $lang that you want to test:
> 
> let $list as element(language)+ := ...
> let $lang as xs:string := 'en'
> 
> Then sort:
> 
> for $i in $list
> order by
>   if ($lang eq 'en') then $i/value[@xml:lang eq $lang] else ()
>   empty least
>   collation "http://marklogic.com/collation/en";,
>   if ($lang eq 'zh') then $i/value[@xml:lang eq $lang] else ()
>   empty least
>   collation "http://marklogic.com/collation/zh";
> return $i
> 
> You can change the behavior around empty values and tweak the collations. You 
> *can't* use a variable as a collation string: for no good reason that I know 
> of, collations in an order-by clause have to be string literals. So there has 
> to be a separate sort key with its own if-then-else and collation for every 
> distinct collation that you want to support. This gets tedious.
> 
> Or you could build a FLWOR as a string and call xdmp:value on it. This way we 
> *can* use a variable for the collation string, but there'll be some overhead 
> from calling xdmp:value and some risk of building an invalid FLWOR expression.
> 
> let $collation :=
>   switch($lang)
>   case 'en' return "http://marklogic.com/collation/en";
>   case 'zh' return "http://marklogic.com/collation/zh";
>   (: Add more cases as needed. :)
>   default return error((), 'UNEXPECTED', $lang)
> let $xqy := 'for $i in $list order by $i/value[@xml:lang eq $lang]'
>   ||' collation "'||$collation||'"'
>   ||' return $i'
> return xdmp:value($xqy)
> 
> -- Mike
> 
> On 10 Jun 2014, at 23:56 , Ashish Bhardwaj <[email protected]> wrote:
> 
> > Hi David,
> >
> > Thanks for your response. Here is the xml:
> > <multilanguage>
> > <language><value xml:lang="zh">丁2橡胶</value> <value 
> > xml:lang="en">third</value></language>
> > <language><value xml:lang="zh">丙酸异辛酯</value> <value 
> > xml:lang="en">tenth</value></language>
> > <language><value xml:lang="zh">丁烯</value> <value 
> > xml:lang="en">first</value></language>
> > <language> <value xml:lang="en">sixth</value></language>
> > <language><value xml:lang="zh">丙酸 丁酯</value> <value 
> > xml:lang="en">seventh</value></language>
> > <language><value xml:lang="zh">丁苯胶</value> <value 
> > xml:lang="en">fourth</value></language>
> > <language><value xml:lang="en">eleventh</value></language>
> > <language><value xml:lang="zh">丁1橡胶</value><value 
> > xml:lang="en">second</value></language>
> > <language><value xml:lang="zh">丙</value> <value 
> > xml:lang="en">1</value></language>
> > <language><value xml:lang="zh">丙~</value> <value 
> > xml:lang="en">fifth</value></language>
> > <language><value xml:lang="zh">丙 乙酯</value> <value 
> > xml:lang="en">eighth</value></language>
> > </multilanguage>
> >
> > I need to sort the above xml based on the value element for xml:lang="zh" 
> > or xml:lang="en" depending on some inputs.
> >
> > How can I do this in MarkLogic.
> > What FLWOR will be needed for both language sorting.
> >
> > Also I am thinking to have to range indexes with different collations(1 for 
> > zh and other for en) is this the right way to solve this.
> >
> > Regards,
> > Ashish
> >
> >
> > On Tue, Jun 10, 2014 at 4:14 PM, David Ennis <[email protected]> wrote:
> > HI.
> >
> > Separate from language support, you can specify the collation to use on 
> > specific string  indexes.
> >
> > https://docs.marklogic.com/guide/search-dev/encodings_collations
> >
> > In my opinion, This is really the magic needed for granular control sorting.
> >
> > As well as defaults, you can define the collation on the index and then 
> > refer to a specific collation in your search.
> >
> > Let me know if this helps.  Otherwise, provide an XML sample and how you 
> > want it sorted.  Then there will be enough information for someone to 
> > provide a proper sample.
> >
> > Kind Regards,
> > David Ennis
> >
> >
> > On 10 June 2014 12:16, Ashish Bhardwaj <[email protected]> wrote:
> > Hi,
> >
> > Can anyone tell me what are the ways of doing a language specific sorting 
> > on a particular data using Marklogic XQuery?
> >
> > I am new to MarkLogic and have gone through a lot of documentation around 
> > collations in Marklogic but how to run a query which can do language 
> > specific sorting is something which is nowhere mentioned in documentation. 
> > I would appreciate if someone can give me a few examples.
> >
> > Thanks,
> > Ashish
> >
> > _______________________________________________
> > General mailing list
> > [email protected]
> > http://developer.marklogic.com/mailman/listinfo/general
> >
> >
> >
> > _______________________________________________
> > General mailing list
> > [email protected]
> > http://developer.marklogic.com/mailman/listinfo/general
> >
> >
> > _______________________________________________
> > General mailing list
> > [email protected]
> > http://developer.marklogic.com/mailman/listinfo/general
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general

_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to