Amplify-cli: GraphQL: @searchable: multiMatch

Created on 2 Mar 2019  Â·  16Comments  Â·  Source: aws-amplify/amplify-cli

Which Category is your question related to?
GraphQL Transform
What AWS Services are you utilizing?
ElasticSearch Service
Provide additional details e.g. code snippets

What is the proper way to use of multiMatch when querying @searchable model. The docs refer to ElasticSearch equivalent but the schema type generated only accepts a single string as a value. Is there any way to handle priority of different fields as shown on Elastic's page?

graphql-transformer pending-close-response-required question

Most helpful comment

Until then, I use this code - which works for me :

const res = await API.graphql(
      graphqlOperation(queries.searchItems, {
        filter: {
          or: [
            { title: { matchPhrasePrefix: query } },
            { content: { matchPhrasePrefix: query } },
          ],
        },
        limit: 20,
        nextToken: nextToken,
      })
    );

All 16 comments

The Elasticsearch multi-match query term accepts a single string as input (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html). There is currently no way to specify the "fields" parameter to the mutli-match query term but you can provide multiple terms as a single space-separated string.

@mikeparisstuff Thank you for clarification.

@mikeparisstuff what does that look like? Im struggling to get it right.

This works,

query SearchCelebs {
  searchCelebs(filter: {first:{matchPhrasePrefix:"Leo"}}) {
    items {
      id
      first
      last
      image
    }
  }
}

should this?

query SearchCelebs {
  searchCelebs(filter: {multiMatch:{query:"Leo", fields:"first last"}}) {
    items {
      id
      first
      last
      image
    }
  }
}

Any update on this? I'm also trying to do a multiMatch with a query and fields and can't seem to get it to work... @mikeparisstuff

An example would be super helpful.

I'm able to get a response with the following call, however, it's returning all items and not filtering based on search params (note: using the Amplify GraphQL API):

const filter = {
  multiMatch: {
    query: search,
    fields: ["name", "id"],
  },
}

try {
  const results = await API.graphql(graphqlOperation(queries.searchApps, filter ))
} catch (e) {
  console.log(e)
}

Curious as to why it's not working as this appears to be calling the filter per the documentation.

Is there any example on how to make it work? I'm also struggling with this. Why this issue was closed?

Why was this issue closed ? has anyone been able to get the syntax working for multi match ?
@jimmyn @raffibag @waltermvp

Can someone please post a working example of using a multimatch GraphQL query that searches for a term in multiple fields. @mikeparisstuff?

After so much fighting with the code, I have an example where I manage to filter several values ​​from the same field, eg, I have 3 values ​​"Canceled", "Waiting", "Sent", where I only want the fields that contain "Waiting" and " Sent"

    not : {
      sms_status : {
       ne  : "Waiting"
      }
      or : {
          sms_status : {
            ne : "Sent"
          }

      }
    },

I hope it helps you

@eliecer2000 Thanks for your response. Not exactly what I was looking for. I wanted to search the same value in multiple fields. But your response helped me craft an initial query that does what I want to do. I will refine it further. As an example if I wanted to do a stock search in both the stock symbol and the stock name I can do something like this. In this example I am sending the lower of the search term to the name field and the upper to the ticker field.

query search {
  searchAssets(filter: { or:
    [ { name: {matchPhrasePrefix:"ama"} }
      { ticker: {matchPhrasePrefix: "AMA" } }
    ]
  },limit: 100,sort: {field:ticker, direction:asc}) {
    items {
        ticker
            name
    }
  }
}

replace [] by {}

query search {
  searchAssets(filter: { 
       or: {
         { name: { matchPhrasePrefix:"ama" }
       },
      { ticker: {matchPhrasePrefix: "AMA" } }
    }
  },limit: 100,sort: {field:ticker, direction:asc}) {
    items {
        ticker
            name
    }
  }
}

@mikeparisstuff
You posted this a year or so ago about multiMatch and the issue was closed. I am having a hard time interpreting your post. Can you post an example using multiMatch? In particular i don't understand what you mean about " provide multiple terms as a single space-separated string." is that to specify the multiple fields?

The Elasticsearch multi-match query term accepts a single string as input (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html). There is currently no way to specify the "fields" parameter to the mutli-match query term but you can provide multiple terms as a single space-separated string.

Any update on this ? As multiMatch is clearly an important feature to use with elasticSearch.

Until then, I use this code - which works for me :

const res = await API.graphql(
      graphqlOperation(queries.searchItems, {
        filter: {
          or: [
            { title: { matchPhrasePrefix: query } },
            { content: { matchPhrasePrefix: query } },
          ],
        },
        limit: 20,
        nextToken: nextToken,
      })
    );

I did the same as @philohelp, so far amplify not supporting multi-query as elastic docs as far I saw.
can you please update when it will support the original elastic query? @raffibag which is something like raffibag done.

Was this page helpful?
0 / 5 - 0 ratings