kyleb740
kyleb740

Reputation: 13

Elasticsearch: Multiple keywords not searching all fields

I am new to elasticsearch and having an issue when searching all fields with multiple keywords. For example if I search all my fields for the term: foo I get all my results back.

But if I search all fields for foo bar it is only returning results from one field that have foo and bar within when I know that there are items with foo in 1 field and bar in another field.

If I search all fields for foo "bar" it will work correctly and return all results from all fields, but really don't want to attempt to explain to users to user quotations on the second term.

Any idea where I am going wrong?

Query:

{
  "query": {
   "bool": {
    "must": {
     "query_string": {
      "fields": ["heading, title, content, call_number"],
      "query": "foo bar",
      "default_operator": "AND"
    }
   }
  }
 }
}

Upvotes: 1

Views: 324

Answers (2)

Bhavya
Bhavya

Reputation: 16192

You can use multi_match with cross_fields type. Try out the below query

{
  "query": {
    "multi_match" : {
      "query":      "foo bar",
      "type":       "cross_fields",
      "fields":     [ "heading", "title" ]
    }
  }
}

Search Results are:

"hits": [
      {
        "_index": "69109590",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "title": "bar"
        }
      },
      {
        "_index": "69109590",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "heading": "foo"
        }
      }
    ]

Upvotes: 1

ExploZe
ExploZe

Reputation: 446

What you are looking for is combined_fields search query

The combined_fields query supports searching multiple text fields as if their contents had been indexed into one combined field. readmore

{
  "query": {
    "combined_fields": {
      "query": "foo bar",
      "fields": [
        "heading",
        "title",
        "content",
        "call_number"
      ],
      "operator": "and"
    }
  }
}

Upvotes: 0

Related Questions