Searching for attributes

46 Views Asked by At

I have the following database in Clusterpoint:

<db>
  <document>
    <id>1</id>
    <name lang="en">John</name>
    <name lang="ru">Джон</name>
  </document>
  <document>
    <id>2</id>
    <name lang="en">Bill</name>
    <name lang="ru">Билл</name>
  </document>
</db>

I am willing to perform a query searching for a specific name. Normally I would specify:

<query><name>Bill</name></query>

However this might match both English and Russian name.

How can I make sure that search would only look inside name with attribute lang="en"?

1

There are 1 best solutions below

3
On

It is possible to define attributes as child tags in request and use them in search, aggregation, ordering etc.

If you want to search for the <name> with certain attribute value, then just specify it in query:

<query><name>Bill<lang>en</lang></name></query>

This will search for documents where both conditions are matched. So, if your database contains, for example, documents (I've added document with id=3):

<db>
  <document>
    <id>1</id>
    <name lang="en">John</name>
    <name lang="ru">Джон</name>
  </document>
  <document>
    <id>2</id>
    <name lang="en">Bill</name>
    <name lang="ru">Билл</name>
  </document>
  <document>
    <id>3</id>
    <name lang="it">Bill</name>
    <name lang="ru">Билл</name>
  </document>
</db>

then query mentioned above (with defined attributes) will return document with id=2.

But if you will search by:

<query><name>Bill</name></query>

then you will get two documents with id=2 and id=3.

Be carefull, if there are repetitive tags, than it is necessary to use tag colocation by defining rule "colocate=yes" in policy configuration for appropriate tag (in your case - <name>), and by using "@" operator in query.

If you add document to your database:

<document>
    <id>4</id>
    <name lang="en">Steve</name>
    <name lang="ru">John</name>
</document>

And will try to return documents, where both conditions match values inside one repetitive tag, then query:

<query><name>John<lang>en</lang></name></query>

will return incorrect result - documents with id=1 and id=4.

But by defining "colocate=yes" for <name> and using "@" operator:

<query> @ <name>John<lang>en</lang></name> @ </query>

you will get correct one - with id=1.