REST API and Conditions


#1

Hi,
I’m using JSON/REST API request for FTS on our Couchbase /index/query. Everything is OK but I would like to query, for example:

(+A +B +C) OR (+A +B +D)

How can I create a query using JSON/REST API?

Thanks in advace.

Angelo


#2

Hey Angelo, this should do it:

curl -XPOST -H "Content-Type: application/json" http://<username>:<passwd>@<ip>:<port>/api/index/<indexName>/query -d
'{"query”:
	{"disjuncts”:
		[
			{“conjuncts”: [{"query": “A"}, {"query": “B”}, {"query": “C”}]},
			{“conjuncts”: [{"query": “A"}, {"query": “B”}, {"query": “D”}]},
		]
	}
}'

#3

Thank you so much for your suggestion!!!

Tomorrow, I’ll test it and I’ll report a feedback on this topic.

Angelo


#4

Hi abhinav,
Thanks a lot for your solution. It works!

Do you know when will be published officially, the release date for Couchbase 5.0?

Thanks.

Angelo


#5

@abhinav
hi
how we can search (ABC) ?

thanks


#6

@angelo.derrico.itcon Apologies for the late reply, couchbase 5.0’s release date is towards the end of this month.


#7

@ytuem96 if you mean A, B, C to be expressions, then your disjunction query would resemble:

curl -XPOST -H "Content-Type: application/json" http://<username>:<passwd>@<ip>:<port>/api/index/<indexName>/query -d
'{"query": {"disjuncts": [{"match": "A"}, {"match": "B"}, {"match": "C"}]}}'

#8

@abhinav
yea if it means and condition,ok


#9

When i run this query from Rest API , i get below error . Only difference is i used different fields instead of A, B , C specific to my project … Any help greatly appreciated

req: &http.Request{Method:“POST”, URL:(url.URL)(0xc4204e0a80), Proto:“HTTP/1.1”, ProtoMajor:1, ProtoMinor:1, Header:http.Header{“User-Agent”:[]string{“Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36”}, “Accept-Encoding”:[]string{“gzip, deflate, br”}, “Connection”:[]string{“keep-alive”}, “Postman-Token”:[]string{“080267ff-9064-1043-1884-076908835068”}, “Authorization”:[]string{“Basic QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==”}, “Cache-Control”:[]string{“no-cache”}, “Origin”:[]string{“chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop”}, “Content-Type”:[]string{“application/json”}, “Accept”:[]string{"/*"}, “Accept-Language”:[]string{“en-US,en;q=0.9”}, “Content-Length”:[]string{“167”}}, Body:ioutil.nopCloser{Reader:(*bytes.Reader)(0xc421fedda0)}, GetBody:(func() (io.ReadCloser, error))(nil), ContentLength:167, TransferEncoding:[]string(nil), Close:false, Host:“127.0.0.1:8094”, Form:url.Values{}, PostForm:url.Values{}, MultipartForm:(*multipart.Form)(nil), Trailer:http.Header(nil), RemoteAddr:“127.0.0.1:63447”, RequestURI:"/api/index/index_central_FTS/query", TLS:(*tls.ConnectionState)(nil), Cancel:(<-chan struct {})(nil), Response:(*http.Response)(nil), ctx:(context.valueCtx)(0xc421fedce0)}, err: alias: QueryAlias parsing searchRequest, req: {
“explain”: true,
“highlight”: {},
“fields”: [
"
"
],
“query”:
[
{“conjuncts”: [{“match”: “Rick”}, {“match”: “Ball”}, {“match”: “Invest”}]}
]
}
, err: json: cannot unmarshal array into Go value of type map[string]interface {}


#10

Looks like your query isn’t valid JSON, and that’s what it’s failing on, lose the square brackets outside the conjuncts construct…

{
"explain": true,
"highlight": {},
"fields": [
	""
],
"query": {
	"conjuncts": [{
		"match": "Rick"
	}, {
		"match": "Ball"
	}, {
		"match": "Invest"
	}]
   }
}

#11

Thanks a lot , it works now …Also i need one more help , i have a requirement , where in user can search for data in multiple different ways and can give search in various forms or combinations like below. how to write these boolean possibilities mixed with a bunch of conjuncts and disjuncts

  1. A AND B AND C
  2. A OR ( B AND C)
  3. (A AND B ) or C
  4. A OR B OR C
  5. (A AND C) OR B

#12
  1. {"query":{"conjuncts":[{"match": "A"}, {"match":"B"}, {"match":"C"}]}}
  2. {"query":{"disjuncts":[{"match": "A"}, {"conjuncts": [{"match":"B"}, {"match":"C"}]}]}}
  3. {"query":{"disjuncts":[{"conjuncts": [{"match":"A"}, {"match":"B"}]}, {"match": "C"}]}}
  4. {"query":{"disjuncts":[{"match": "A"}, {"match":"B"}, {"match":"C"}]}}
  5. {"query":{"disjuncts":[{"conjuncts": [{"match":"A"}, {"match":"C"}]}, {"match": "B"}]}}

#13

Does FTS support searching elements from within a array of Json objects ?Its like , i have a json object like below which has array of Json objects , i need FTS search( Exact Match) based off the “name” and sample values(Restaurant123 etc) that i listed . I am getting a whole unmarshall errors , tried a conjuncts query, however it is also returning other partial matches as well instead of exact match … Is there special way to query an Array of json objects ?
My json
{
“Restaurants”: [
{
“id”: 1
“name”: “Panda Express Fastfoods”,
},
“id”: 3
“name”: “Restaurant123”,
},

]

}
Search query i give is below

“query”: {
“conjuncts”: [{
“match”: “Panda”
}, {
“match”: “express”
}, {
“match”: “FastFoods”
}]
}


#14

If you’ve built a default index, without any specific type mappings, to search for those entries in select fields only and to avoid any partial results - you’d need to explicitly specify the ‘field’ section. So you’re query would look something like this:

"query": {
    "conjuncts": [
        {
            "match": "Panda", "field": "Restaurants.name"
        },
        {
            "match": "express", "field": "Restaurants.name"
        },
        {
            "match": "FastFoods", "field": "Restaurants.name"
        }
    ]
}

You can skip the field section and avoid partial results if you’ve created an index with the exact mapping that you want to search over.


#15

Above does not work . In my case i have created an index with the exact mapping . The Restaurants Array of Json objects is just one sub object in a big object . The query results exactly pull the matched record on name , however the output lists out the entire array of names as opposed to just the specific matched name …this problem occurs only for those seraches that you do on Arrray of Json objects …The issue does not exist if you UNNEST or keep the Restaurants as seperate doctype.

However via NQL query if i give below , it will give me the exact match in the array . Can i run an NQL query over a FTS index ?
SELECT ARRAY v FOR v IN restaurants WHEN v.name = “Panda Express” END AS hotel_platform
FROM Bucket Name
WHERE docType=“some sample doc type…”
AND ANY v IN restaurants SATISFIES v.name = “Continuous Delivery Platform” END;


#16

In FTS the unit of a match is the document. So, while it can search within the contents of an array, if it finds something, the entire document is considered a match.


#17

aha , got it , thats why its returning the entire document for me …so based off capabilities that couchbase supports now , looks like i need to move it under different docType …
One other question i have is can i run a NQL query like the one above on a FTS index ?


#18

Currently N1QL queries cannot make use an FTS index.