how to search for tags in elasticsearch
Asked Answered
A

1

12

I am creating search for photo gallery project where photos could have upto 50 tags (just like in shutterstock and fotolia). I am creating my search in elasticsearch. I have a field with datatype keyword in elasticsearch. When query comes like "Abstract Background", I want to search for abstract and background in all the keywords of the images and sort them by their relevancy. It should not match abstr backgrou. I wrote a query like this

 "query": {
    "bool": {
      "should": [
        {
          "match": {
            "keyword": {
              "query": "abstract, background"
            }
          }
        }
      ]
    }
  }

It only works for matching single keywords. I want to match for multiple keywords and also sort them by their relevancy. Thanks

-----EDIT------

These are my mappings. Title field works fine. Category is just used for aggregations and keyword is the main field to match.

PUT /freevects
{
  "mappings": {
    "photos": {
      "properties": {
        "title": {
          "type": "text",
          "boost": 1.9,
          "analyzer": "standard"
        },
        "keyword": {
          "type": "keyword",
          "boost": 1.4
        },
        "category": {
          "type": "keyword",
          "index": false
        },
        "quality": {
          "type": "short",
          "index": false,
          "boost": 1.1
        },
        "downloads": {
          "type": "integer",
          "index": false,
          "boost": 1.1
        },
        "likes": {
          "type": "integer",
          "index": false,
          "boost": 1
        },
        "filename": {
          "type": "keyword",
          "index": false
        },
        "type": {
          "type": "keyword",
          "index": false
        },
        "free": {
          "type": "short",
          "index": false
        },
        "created": {
          "type": "date",
          "index": false
        }
      }
    }
  }
}
Adiabatic answered 30/3, 2017 at 9:22 Comment(2)
Could you please post the mappings for the index that you are searching on? I'd be able to help with those in hand.Hospitality
I have added the mapping, please check it out. Any questions please ask me.Adiabatic
H
12

The problem is with the mapping of keyword field. It is of type: keyword in your mapping. This doesn't tokenize your search query and the indexed values. So when you search, the terms are searched as is.


Example:

Searching for: "abstract, background" (as you did in your question), will actually search only exact occurrences of "abstract, background" in the keyword field.

Change the mapping of keyword field to type: text

"keyword": {
  "type": "text",
  "boost": 1.4
}

And index your values as:

{
  "keyword": ["abstract", "background"]
}

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-keyword-analyzer.html


Query for searching tags:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "keyword": "abstract"
          }
        },
        {
          "match": {
            "keyword": "background"
          }
        }
      ]
    }
  }
}
Hospitality answered 30/3, 2017 at 13:11 Comment(4)
In elasticsearch 5 string datatype has been deprecated. I used text instead of it and it works fine for indexing the documents as you said. But how could I search for the query that searches for abstract keyword and background keyword in all the images and sort by their relevancy. If I use two keywords it in the query like this "query": { "bool": { "should": [ { "match": { "keyword": { "analyzer": "keyword", "query": "abstract, background" } } } ] } } It do not work.Adiabatic
Why are you using "analyzer" : "keyword" in your query? Using keyword analyzer would try to match your query as it is, so: "abstract, background". You want it to match the query as: "abstract", "background"Hospitality
I'd advise you to read a bit more about Elasticsearch Analyzers. Let me know if you face any problems. ThanksHospitality
Thanks I changed my analyzer to standard and it works fine.Adiabatic

© 2022 - 2024 — McMap. All rights reserved.