How to return document fields in Nodejs (3.0)

I’m using NodeSdk 3.0

async function search(query, limit, skip) {
  try {
    const result = await cluster.searchQuery(
      "test-search",
      couchbase.SearchQuery.queryString(query),
      { fields: ["title"] }
    );

    return result.rows;
  } catch (error) {
    console.error(error);
  }
}

the result not returning the title field.

How to return document fields from searchQuery?

1 Like

Hi Marsh,

This forum question needs a little more work, but I’m going to try and help you out anyways. I’ll take a stab at this and respond back shortly.

If I understand correctly, you are working with Full Text Search, and you are trying to return just one of the fields from the document(s) that match your search?

One request, can you give us an example of what your query looks like?

Now I’m returning documents with collection.get after searchQuery

    const result = await cluster.searchQuery("test-search", qp, {
      limit,
      skip,
    });

    if (result.rows.length === 0) return [];

    const results = await Promise.all(
      result.rows.map(async (row) => {
        const doc = await collection.get(row.id, {
          project: [
            "type",
            "title",
            "price",
            "photos",
            "location",
            "createdAt",
          ],
        });
        return { ...doc.content, id: row.id };
      })
    );

Can I able to return document fields with searchQuery function? without querying documents one by one. Currently searchQuery function returning only the search hit id I need to make searchQuery function return documents fields with id eg: [id, title, price]

thank you @ericb

1 Like

OK, you can do this for sure, as I had suggested in the previous comment.

However, if you want to just return one field you can select the [x]store option in the FTS Index. Let me show a good example using the travel-sample data set.

async function ftsMatchWord(searchQueryString) {
  try {
    const result = await cluster.searchQuery(
      "index-hotel-description",
      couchbase.SearchQuery.queryString(searchQueryString),
      { limit: 5, fields: ["description"] }
    )
    return result.rows;
  } catch (error) {
    console.error(error)
  }
}

ftsMatchWord("+description:pool -continental breakfast")
  .then((result) => console.log(result))

In my FTS Index I have it set up like so:

Let me know if between these two solutions you are able to do what you are wanting, otherwise let me know if there is anything else I can help with.

1 Like

@ericb thanks that’s what I was looking for, And can I get a nested object field along with other fields?

This is a sample document. How to return the location.district with other fields?

{
  "type": "SELL",
  "category": {
    "field": "property",
    "item": "houses & apartments"
  },
  "location": {
    "district": "Batticaloa",
    "city": "Batticaloa"
  },
  "title": "The first thing I have done",
  "price": 343444,
  "fields": {
    "property type": "apartment",
    "furnishing": "fully-furnished",
    "negotiable": true,
    "floors": "25",
    "landSize": {
      "area": "500",
      "unit": "arches"
    }
  },
  "createdAt": "2020-11-03T14:58:21.648Z",
  "creator": {
    "name": "John Doe ",
    "id": "caa8f854-fd49-43d3-b603-64f8f483a701"
  },
  "id": "-AePUFUwb5kLnNLST4yIx",
}

What I tried.

    const result = await cluster.searchQuery("test-search", qp, {
      limit,
      skip,
      fields: ["type", "title", "price", "photos", "location", "createdAt"],
    });

But this only returns fields createdAt, photos, price, title, type

Hi Chawki,

I didn’t realize we still had an issue here, I will take a look at this question as well.