0

I have structure of data like this

    "id": "162d2df9-16e0-4171-bc54-52f2b3a4a23c",
"uri": "https://local.test.com/abc",
"title": "Geography",
"dateTime": "2022-06-15T09:32:37+00:00",
"type": "Default",
"hcs": "Primary",
"lE": "1",
"otherData": {
        "id": "57907298-3945-4654-a75a-7278bcb14399",
        "uri": "https://local.test.com/gcsp",
        "title": "GCSP"
    },
"moreData": {
    "id": "48e939c5-c4f3-4cf2-ae2d-8d964682361d",
    "uri": "https://local.test.com/xyz",
    "title": "Default"
},
"Hierarchy": {
    "data": {
        "id": "57907298-3945-4654-a75a-7278bcb14399",
        "title": "GCSP"
    },
    "parent": [
        {
            "id": "162d2df9-16e0-4171-bc54-52f2b3a4a23c",
            "title_fs": "Maths",
            "hcs_p": "Primary",
            "parent": [
                {
                    "id": "ddff7857-f8be-45a1-b41d-aef2207ff66b",
                    "title_fs": "Civics",
                    "hcs_p": "Primary",
                    "parent": [
                        {
                            "id": "24d126e2-464b-499b-b2c0-adcd7a7b7728",
                            "title_fs": "History",
                            "hcs_p": "Primary",
                            "parent": [
                                {
                                    "id": "23a094da-33b8-4960-8daa-8291e6634305",
                                    "title_fs": "Science",
                                    "hcs_p": "Primary",
                                    "parent": [
                                        {
                                            "id": "32de28ab-5bd5-4cd6-9dc9-f9766fcdfab3",
                                            "title_fs": "Subjects",
                                            "hcs_p": "Primary",
                                            "parent": []
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

And I want to exclude "Hierarchy" data to be searched and filter in result set so how can I achieve that?

I have tried: ['bool']['must'][] = [ "multi_match" => [ "operator" => "and", "query" => $searchTerm, "fields" => ["*", "Hierarchy"] ] ];

but not working

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • Maybe it can help https://typesense.org/learn/elasticsearch-exclude-fields/ ? – Monnomcjo Jun 24 '22 at 11:07
  • source will exclude from response, will that exclude from search too? @Monnomcjo – Bhumi Shah Jun 24 '22 at 11:59
  • no :( The _source/exclude setting is only useful for not returning the body field in the response. Maybe this post is more usefull https://stackoverflow.com/questions/39451688/exclude-a-field-on-a-elasticsearch-query – Monnomcjo Jun 24 '22 at 12:08
  • so I tried like this $params["_source"] = ["exclude" => 'Hierarchy']; but it is still searching inside hierarchy @Monnomcjo – Bhumi Shah Jun 24 '22 at 12:25
  • @BhumiShah `_source` `exclude` will just exclude from result and not from query. – Sagar Patel Jun 24 '22 at 13:14

1 Answers1

0

There is no way to exclude specific field while searching, but you can provide list of field in which you want to search.

{
  "query": {
    "multi_match": {
      "query": "searchTerm",
      "fields": ["field1","field2","field3"], <-- this will search to field1, field2, field3 only
      "operator": "and"
    }
  }
}

You can use excludes param inside _source if you want to exclude field from response:

{
  "_source": {
    "excludes": ["field4"]
  }, 
  "query": {
    "multi_match": {
      "query": "searchTerm",
      "fields": ["field1","field2","field3"],
      "operator": "and"
    }
  }
}
Sagar Patel
  • 4,993
  • 1
  • 8
  • 19