Istvano
Istvano

Reputation: 982

Elasticsearch simple query string query vs exact text search

I have a document with multiple fields.

document

I would like to search in using google like search e.g.

when I use "blue car" it must be an exact value match without any analysis at all. it must only find the exact phrase without stemming etc.

for this I got a working version like this ->

{
  "query": {
    "multi_match": {
      "query": "blue car",
      "fields": [
        "text",
        "message",
        "whatever"
      ],
      "type": "phrase"
    }
  }
}

Although for the other kind of search like -diesel or +hybrid. I was thinking about using the as that seems to support that well.

{
   "query": {
        "simple_query_string": {
           "fields": [
            "text",
            "message",
            "whatever"],
           "default_operator": "and",
           "query": "-diesel +hybrid"
        }
     }
}

unfortunately simple query string query will use an analyser even when I put the text in quotes in the query.

Do you guys have an idea how I could combine the two perhaps ? Should Can I use a bool query to combine them together ?

Upvotes: 1

Views: 3571

Answers (1)

Dan Tuffery
Dan Tuffery

Reputation: 5924

Here's an example of the query "blue car" -diesel +hybrid' using the bool query

{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "blue car",
                        "fields": [
                            "text",
                            "message",
                            "whatever"
                        ],
                        "type": "phrase"
                    }
                },
                {
                    "multi_match": {
                        "query": "hybrid",
                        "fields": [
                            "text",
                            "message",
                            "whatever"
                        ]
                    }
                }
            ],
            "must_not": {
                "multi_match": {
                    "query": "diesel",
                    "fields": [
                        "text",
                        "message",
                        "whatever"
                    ]
                }
            }
        }
    }
}

Upvotes: 6

Related Questions