1
{
 "query": {
  "bool": {
    "must": {
      "bool": {
        "must": [
          {
            "multi_match": {
              "query": [
                "869336","45345345"
              ],
              "type": "phrase_prefix",
              "fields": [
                "id",
                "accountNo",
                "moblileNo"
              ]
            }
          }
        ],
        "should": [],
        "must_not": []
      }
    },
    "filter": {
      "bool": {
        "must": {
          "bool": {
            "must": [],
            "should": [],
            "must_not": []
          }
        }
      }
    }
  }
}

}

Need to use multi match query only.I have mentioned some sample fields in the query.I get below error when I run it on postman : [multi_match] unknown token [START_ARRAY] after [query]

1 Answer 1

1

For the error that is because query only takes a single input string.

It can have multiple values like "query" : "869336 45345345" however with the values separated by spaces. How this works, you can probably go through this link.

Now looking into your scenario, assuming you'd want to apply phrase match queries for both the values i.e. 869336 and 45345345, you just need to separate out the values in its individual multi-match queries.

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "869336",
                "type": "phrase_prefix",
                "fields": [
                  "accountNo",
                  "moblileNo"
                ]
              }
            },
            {
              "multi_match": {
                "query": "45345345",
                "type": "phrase_prefix",
                "fields": [
                  "accountNo",
                  "moblileNo"
                ]
              }
            }
          ],
          "should": [],
          "must_not": []
        }
      },
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "must": [],
              "should": [],
              "must_not": []
            }
          }
        }
      }
    }
  }
}

Now if you do not want to apply phrase_prefix and instead want to return all the documents having both values in any of the fields, you can simply write the query as below:

POST my-multimatch-index/_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "869336 45345345",
                "fields": [
                  "accountNo",
                  "moblileNo"
                ],
                "type": "cross_fields",              <--- Note this
                "operator": "and"                    <--- This would mean only return those documents having both these value. 
              } 
            }
          ],
          "should": [],
          "must_not": []
        }
      },
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "must": [],
              "should": [],
              "must_not": []
            }
          }
        }
      }
    }
  }
}

Note the above comments and how I made use of cross-fields. Best to go through the links to get better understanding.

Anyways feel free to ask for questions and I hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

I have one name string with spaces and another string at this point how I will differenciate it.
You can simply make use of single quote something like this "query": "869336 '45345345 1234'"
@AbhijeetSanap If you think your query has been resolved, please upvote and/or accept the answer as mentioned in this link. This would help any other users reading your question understand that the query has been resolved.
Actually I am getting an issue I have 2 accounts having account name as DCC 1 and DCC 2 .....now I want to find DCC 1 so I am passing query:"'DCC 1'" but getting both results even after giving single quote inside double quote

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.