Siddhartha Chowdhury
Siddhartha Chowdhury

Reputation: 2734

Elasticsearch Search query with multiple filters

Hello guys I am new to elastic search but I have gone through the basic ElasticSearch 5.1 documentation.

Problem in one line:

Search is successful but filters are not working properly.

Mapping datatypes

{
    "properties": {
        "title": {"type": "string"},
        "description": {"type": "string"},
        "slug": {"type": "string"},
        "course_type": {"type": "string", "index" : "not_analyzed"},
        "price": {"type": "string"},
        "categories": {"type": "keyword", "index" : "not_analyzed"},
        "tags": {"type" : "keyword"},
        // "tags": {"type" : "keyword", "index" : "not_analyzed"},
        "status": {"type" : "string","index" : "not_analyzed"},
    }
}

As noted by @Darth_Vader I tried mapping as well. Following is my mapping

Document in index (Req-1)

....
{
    "_index": "learnings",
    "_type": "materials",
    "_id": "582d9xxxxxxxx9b27fab2c",
    "_score": 1,
    "_source": {
      "title": "Mobile Marketing",
      "slug": "mobile-marketing",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eleifend hendrerit vehicula.",
      "categories": [
        "Digital Marketing"
      ],
      "tags": [
        "digital-marketing",
        "mobile-marketing"
      ],
      "status": "published"
    }
},
...

Like above I have like hundred documents in an index

SEARCH QUERY FULL that I am using

"query": {
    "bool": {
        "must": {
             "multi_match" : {
              "query" : "mobile",
              "fields" : [ "title^5", "tags^4", "categories^3" ],
              "operator": "and"
            }
        },
        "filter": {
            "bool" : {
                "must" : [
                    {"term" : {"status": "published"} }
                ]
             }
         }
    }
}

NOW the problem is:

If I modify my Search Query and add my required filter like the following below: I get NO search result (hits = 0) even though my document (Req-1) has mobile-marketing as a tag

"filter": {
    "bool" : {
        "must" : [
            {"term" : {"status": "published"} },
            {"term" : {"tags": "mobile-marketing"} }
        ]
     }
 }

BUT if I change the filter {"tags": "mobile-marketing"} TO {"tags": "mobile"}, I get the required document (Req-1) as result.

I want to get the same document using this filter: {"tags": "mobile-marketing"}. So where am I doing wrong?

What modification does my search query need?

Thanks

Upvotes: 1

Views: 1760

Answers (1)

Kulasangar
Kulasangar

Reputation: 9464

How does your mapping look for tags?

Seems like you've got your mapping for tags field as analyzed. What *analyzed` does is, from the books:

First analyze the string and then index it. In other words, index this field as full text.

So it analyzes it first, where the value looks like mobile-marketing. Hence it'll store mobile and marketing separately because of the hyphen in the middle and it'll be tokenized into tokens. ie: it'll store mobile and marketing into two different tokens.

Whereas if it's not_analyzed:

Index this field, so it is searchable, but index the value exactly as specified. Do not analyze it.

So this will basically store the value as it is without analyzing it, which should do the trick in your case. Maybe you should have a look at this point as well.

Hope it helps!

Upvotes: 1

Related Questions