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
}
}
}
}
}
You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .
Tagging is a common design pattern that allows us to categorize and filter items in our data model. In this article, we'll implement tagging using Spring and Elasticsearch. We'll be using both Spring Data and the Elasticsearch API.
There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.
Elasticsearch takes in unstructured data from different locations, stores and indexes it according to user-specified mapping (which can also be derived automatically from data), and makes it searchable. Its distributed architecture makes it possible to search and analyze huge volumes of data in near real time.
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"
}
}
]
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With