chattrat423
chattrat423

Reputation: 603

multi word query in elasticsearch with filter

I put a dataset into ES with the following fields:

Category,Question,Answer

Trying to query my elasticsearch index for the phrase "world series" in either the Question field OR the Answer field, but it also must ONLY limit its results/search to the Category I specify

{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"Question" : "world series"}},
                 { "term" : {"Answer" : "world series"}} 
              ],
              "must" : {
                 "term" : {"Category" : "Sport"} 
              }
           }
         }
      }
   }
}'

When I do this, however, it does NOT let me use multiple words (i.e. 'world series').

How can I search for phrases/terms but ONLY search the Category (i.e. Sport) I want?

Upvotes: 0

Views: 3832

Answers (2)

eyildiz
eyildiz

Reputation: 343

You can add an extra field which named Searchtext which is analyzed and Searchtext may include Category, Question, Answer

Then;

{
"query": {
    "constant_score": {
        "filter": {
            "bool": {
                "must": {
                    "term": {
                        "Category": "Sport"
                    },
                    "match": {
                        "SearchText": {
                            "query": "world series",
                            "operator": "and"
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 0

blackmamba
blackmamba

Reputation: 556

Try this

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "Category": "Sport"
          }
        },
        {
          "multi_match": {
            "query": "world series",
            "type": "cross_fields",
            "operator": "and",
            "fields": [
              "Question",
              "Answer"
            ]
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

Upvotes: 1

Related Questions