5

I am recently using elasticsearch in a website. The scenario is, I have to search a string on afield. So, if the field is named as title then my search query was,

"query" :{"match": {"title": my_query_string}}.

But now I need to add another field in it. Let say, category. So i need to find the matches of my string which are in category :some_category and which have title : my_query_string I tried with multi_match. But it does not give me the result i am looking for. I am looking into query filter now. But is there way of adding two fields in such criteria in my match query?

2
  • How do you define the category? Is it something that the user will specify or something that will be implied? eg. Say you have a bookstore, and the user now browses the "fantasy" category. If he searches you want only to search the titles under this category? Commented Apr 7, 2014 at 10:28
  • yes. category is another field like the title. Commented Apr 7, 2014 at 10:32

3 Answers 3

10
GET indice/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "title"
          }
        },
        {
          "match": {
            "category": "category"
          }
        }
      ]
    }
  }
}

Replace should with must if desired.

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

Comments

3

Ok, so I think that what you need is something like this:

"query": {
    "filtered": {
        "query": {
            "match": {
                "title": YOUR_QUERY_STRING,
            }
        },
        "filter": {
            "term": {
                "category": YOUR_CATEGORY
            }
        }
    }
}

If your category field is analyzed, then you will need to use match instead of term in the filter.

Comments

0
      "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {"match": {"title": "bold title"},
                            {"match": {"body": "nice body"}}
                        ]
                    }
                },
                "filter": {
                    "term": {
                        "category": "xxx"
                    }
                }
            }
        }

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.