0

Suppose I have documents with the following structure.

{
  "text": "This is the road I mentioned",
  "created_at": "2015-01-11T05:30:36.000Z",
  "language": "en",
  "character_count": 25,
  "userInfo": {
    "id": 553669398108446700,
    "user_id": 2803103316,
    "user_screen_name": "blue555555"
  }
}

I need to search for a keyword say "road" in the field "text" and another keyword "blue555555" in the field "user_screen_name". This is basically searching on multiple fields, how can I do that in elasticsearch?

2 Answers 2

1

You can use a combination of match and term queries inside a bool/must query so that both constraints match.

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "text": "road"
          }
        },
        {
          "term": {
            "userInfo.user_screen_name": "blue555555"
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

As one of the search is of query behavior and other is filter behavior , I would suggest filtered query.

{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "text": "road"
        }
      },
      "filter": {
        "term": {
          "userInfo.user_screen_name": "blue555555"
        }
      }
    }
  }

}

Comments

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.