1

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"} }
                ]
             }
         }
    }
}
  • In the above query the most important search criteria/filter is {"term" : {"status": "published"} }. Every search result must meet this requirement.

  • Now from the list of results, I want to filter more. So say I want to get only documents which has mobile-marketing as a tag. My document (Req-1) has this tag (mobile-marketing)

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

1
  • [For people having similar issue].The above procedure and the related code works, make sure you use proper index name and index types at time of mapping and other related operations Commented Feb 1, 2017 at 10:42

1 Answer 1

1

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!

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

6 Comments

Oh thanks @Darth_vader, I didnt do any mapping, I simply created an index and started populating the document without any mapping. I guess I need to do the mapping and make it "type" : "string", "index" : "not_analyzed" . Let me check if it works. Thanks
Yup sure, let me know if it works @SiddharthaChowdhury.
Hey @Darth_Vader, I tried mapping and added my mapping details in the question . But it didnot worked. Its behaving the same way.
What if you have tags type as text? "tags": {"type" : "text", "index" : "not_analyzed"}
And if it the type is keyword you don't have to explicitly set not_analyzed since keyword is suppose to have it by default. Could be helpful. Let me know if it works.
|

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.