最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Search array with AND azure cognitive search - Stack Overflow

programmeradmin6浏览0评论

I'm trying to figure out how to query in azure search using arrays with multiple matching values using AND logic.

The following query will return values matching categoryOne OR categoryTwo the boost score will be correctly sorted with the highest values having both at the top but i was wondering how i would restructure my query to use AND logic so it only returns documents where the field includes categoryOne and categoryTwo.

$filter=Categories/any(c: c eq 'categoryOne') AND Categories/any(c: c eq 'categoryTwo')

I've tried using multiple different queries but can't get it to work with the correct AND logic

s.searchIn('Categories', 'categoryOne') AND s.searchIn('Categories', 'categoryTwo')

I want to eventually have queries where i can combine this logic to find documents that have (categoryOne or categoryTwo) AND (categoryThree, or categoryFour or categoryFive) but struggling with getting AND to work.

This is the field property for my index

{
  "name": "Categories",
  "type": "Collection(Edm.String)",
  "searchable": true,
  "filterable": true,
  "retrievable": true,
  "stored": true,
  "sortable": false,
  "facetable": true,
  "key": false,
  "synonymMaps": []
}

I'm trying to figure out how to query in azure search using arrays with multiple matching values using AND logic.

The following query will return values matching categoryOne OR categoryTwo the boost score will be correctly sorted with the highest values having both at the top but i was wondering how i would restructure my query to use AND logic so it only returns documents where the field includes categoryOne and categoryTwo.

$filter=Categories/any(c: c eq 'categoryOne') AND Categories/any(c: c eq 'categoryTwo')

I've tried using multiple different queries but can't get it to work with the correct AND logic

s.searchIn('Categories', 'categoryOne') AND s.searchIn('Categories', 'categoryTwo')

I want to eventually have queries where i can combine this logic to find documents that have (categoryOne or categoryTwo) AND (categoryThree, or categoryFour or categoryFive) but struggling with getting AND to work.

This is the field property for my index

{
  "name": "Categories",
  "type": "Collection(Edm.String)",
  "searchable": true,
  "filterable": true,
  "retrievable": true,
  "stored": true,
  "sortable": false,
  "facetable": true,
  "key": false,
  "synonymMaps": []
}
Share Improve this question edited Mar 26 at 3:21 Suresh Chikkam 3,6062 gold badges4 silver badges12 bronze badges Recognized by Microsoft Azure Collective asked Mar 21 at 14:18 CartionCartion 1794 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The reason for the above query does not work is that Categories/any(c: c eq 'categoryOne') AND Categories/any(c: c eq 'categoryTwo') returns documents where at least one category equals categoryOne AND at least one category equals categoryTwo. this does not check that both values exist in the same array.

  • Use all instead of any. This query checks that both categoryOne and categoryTwo exist in the array.
$filter=Categories/all(c: c ne 'categoryOne') eq false AND Categories/all(c: c ne 'categoryTwo') eq false
  • If you want to extend this to (categoryOne OR categoryTwo) AND (categoryThree OR categoryFour OR categoryFive).
$filter=(Categories/all(c: c ne 'categoryOne') eq false OR Categories/all(c: c ne 'categoryTwo') eq false) 
AND (Categories/all(c: c ne 'categoryThree') eq false OR Categories/all(c: c ne 'categoryFour') eq false OR Categories/all(c: c ne 'categoryFive') eq false)
发布评论

评论列表(0)

  1. 暂无评论