0

I want to create an index on qualityConfirmations attribute and query it. As qualityConfirmations has dynamic fields at the root level for eg. 228, 234, and inside that I need to query for "qualityConfirmation.qualityConfirmationComment", "qualityConfirmation.inspectDate" and other fields.

  {
    "_id": "4fa10c9b-099b-4269-92c9-ed22095f80c5",
    "qualityConfirmations": {
      "228": {
        "qualityConfirmation": {
          "delayGroupCode": "05",
          "estimatedDelayDays": 42,
          "inspectDate": {
            "$date": "2022-04-26T00:00:00.000Z"
          },
          "qualityConfirmationCode": "R",
          "qualityConfirmationComment": "QUALITY CONFIRMATION ACTIVITIES",
          "qualityConfirmationDescription": "RELEASED",
          "qualityConfirmationYear": 2022,
          "repairDate": {
            "$date": "2022-05-13T00:00:00.000Z"
          },
          "stopSaleNumber": "228"
        }
      },
      "234": {
        "qualityConfirmation": {
          "delayGroupCode": "40",
          "estimatedDelayDays": 294,
          "inspectDate": {
            "$date": "2022-05-26T00:00:00.000Z"
          },
          "qualityConfirmationCode": "R",
          "qualityConfirmationComment": "QUALITY CONFIRMATION ACTIVITY",
          "qualityConfirmationDescription": "RELEASED",
          "qualityConfirmationYear": 2022,
          "repairDate": {
            "$date": "2022-09-23T00:00:00.000Z"
          },
          "stopSaleNumber": "234"
        }
      }
    }
  },
  {
    "_id": "4fa10c9b-099b-4269-92c9-ed22095f80c6",
    "qualityConfirmations": {
      "237": {
        "qualityConfirmation": {
          "delayGroupCode": "39",
          "estimatedDelayDays": 287,
          "inspectDate": {
            "$date": "2022-06-02T00:00:00.000Z"
          },
          "qualityConfirmationCode": "R",
          "qualityConfirmationComment": "QUALITY CONFIRMATION ACTIVITY",
          "qualityConfirmationDescription": "RELEASED",
          "qualityConfirmationYear": 2022,
          "repairDate": {
            "$date": "2022-11-07T00:00:00.000Z"
          },
          "stopSaleNumber": "237"
        }
      },
      "246": {
        "qualityConfirmation": {
          "delayGroupCode": "34",
          "estimatedDelayDays": 252,
          "inspectDate": {
            "$date": "2022-07-19T00:00:00.000Z"
          },
          "qualityConfirmationCode": "R",
          "qualityConfirmationComment": "QUALITY CONFIRMATION ACTIVITY",
          "qualityConfirmationDescription": "RELEASED",
          "qualityConfirmationYear": 2022,
          "repairDate": {
            "$date": "2022-09-23T00:00:00.000Z"
          },
          "stopSaleNumber": "246"
        }
      }
    }
  },
  {
    "_id": "4fa10c9b-099b-4269-92c9-ed22095f80c7",
    "qualityConfirmations": {
      "265": {
        "qualityConfirmation": {
          "delayGroupCode": "19",
          "estimatedDelayDays": 147,
          "inspectDate": {
            "$date": "2022-10-19T00:00:00.000Z"
          },
          "qualityConfirmationCode": "R",
          "qualityConfirmationComment": "ABC CONFIRMATION ACTIVITY",
          "qualityConfirmationDescription": "RELEASED",
          "qualityConfirmationYear": 2022,
          "repairDate": {
            "$date": "2022-11-08T00:00:00.000Z"
          },
          "stopSaleNumber": "265"
        }
      }
    }
  }
]

I've used below schema and query for same.

  "mappings": {
    "dynamic": true,
    "fields": {
      "qualityConfirmations": {
        "type": "document",
        "dynamic": true,
        "fields": {
          "*": {
            "dynamic": true,
            "type": "document",
            "fields": {
              "qualityConfirmation": {
                "type": "document",
                "dynamic": false,
                "fields": {
                  "qualityConfirmationComment": {
                    "type": "string",
                    "analyzer": "earmarkAnalyzer"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "analyzers": [
    {
      "charFilters": [],
      "name": "earmarkAnalyzer",
      "tokenFilters": [],
      "tokenizer": {
        "type": "keyword"
      }
    }
  ]
}



**QUERY:**

[
  {
    $search: {
      index: "default",
      compound: {
        must: [
          {
            phrase: {
              path: {
                wildcard:
                  "qualityConfirmations.*.qualityConfirmation.qualityConfirmationComment"
              },
              query: ["ABC CONFIRMATION"],
            }

          },
        ],
        mustNot: [],
        should: [],
        minimumShouldMatch: 0,
      },
    },
  },

]

But the result is not coming as expected. For example, If I want to use exact match , it still brings up all the results matching input, I've also used KEYWORD analyzer to create single token but it is not working. I want to search for both string and date Types.

Please find below the atlas playground link for same .

https://search-playground.mongodb.com/tools/code-sandbox/snapshots/676030e03206573cca5920b8

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.