{
"title" : "That Uselessly Amazing Title",
"author" : "Someone you have never heard of",
"url" : "http://www.theuselessweb.com",
"summary" : "a collection of useless websites",
"tag" : ["useless","maybe useful"]
}
Say I have a schema that looks like the one shown above. The user asks the application to show something "useless".
How do I write a query that will look through the title, summary, and tags for the word "useless" as a fuzzy search?
From the docs Fuzzy match Query
GET /my_index/my_type/_search
{
"query": {
"multi_match": {
"fields": [ "summary", "title", "tag" ],
"query": "useless",
"fuzziness": "AUTO"
}
}
}
This query works because it's using a multi_match query
Fuzziness works only with the basic match and multi_match queries. It
doesn’t work with phrase matching, common terms, or cross_fields
matches.
Otherwise you'll have to combine several fuzzyqueries inside a bool Query
Related
What I need is, elastic should search in multiple fields and return data by field priority.
For example: For the search string obil hon, elastic should search in fields[title, description, modelCaption] and return data when at first it finds Mobile Phone in Title field, then in other fields.
Query I use:
{
"from": 0,
"query": {
"bool": {
"must": [
{
"query_string": {
"default_operator": "or",
"fields": [
"title^5",
"description",
"modelCaption",
"productFeatureValues.featureValue",
"productFeatureValues.featureCaption"
],
"query": "*obil* *hone*"
}
}
]
}
},
"size": 16
}
Any suggestions?
Thanks!
You can simply use the multi-match query to query multiple fields and it supports boosting a particular field like a title in your case and different operators like OR in your case.
Sample ES query for your use case:
{
"query": {
"multi_match" : {
"query" : "mobile phones",
"fields" : [ "title^5", "description","modelCaption","productFeatureValues.featureVal"],
"fuzziness" : "AUTO" --> Adding fuzziness to query
}
}
}
Here title filed is boosted by factor 5, hence if mobile phones match in title field then it would be scored higher.
Also please note, you are using wild-card in your query string which is very costly so it's better to avoid them if you can.
EDIT: Based on OP comments, included fuzziness parameter AUTO in query for better results
How can we configure elastic search so that it only returns results which matches all the words in the search query. The documents indexed have data having multiple fields and so the words of search query may match different fields of data but all the words must get matched in the result ?
you can query string query feature to search for results
sample search query
GET /_search
{
"query": {
"query_string": {
"query": "(content:this OR name:this) AND (content:that OR name:that)"
}
}
}
In this query content and name is the field name, this is the search criteria
you can build search query similar to that.
I think you're looking for a multi_match query together with and operator. This is the link to docs: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html and it seems that cross_fieldsis query type you're looking for. I'd read more on that page, but this is probably what you are looking for:
GET /_search
{
"query": {
"multi_match" : {
"query": "Will Smith",
"type": "cross_fields",
"fields": [ "first_name", "last_name" ],
"operator": "and"
}
}
}
I'm using cutoff_frequency in a multi_match query with multiple fields. Is it applied to every field individually? How does it work?
This is what my code looks like.
POST beta2_index/_search
{
"_source": ["title"],
"size": 20,
"query": {
"multi_match": {
"query": "test query",
"fields": [
"title",
"description"],
"cutoff_frequency" : 0.1
}
}
}
multi_match query with option best_fields type (the default), is transformed into a dis_max query that wraps match queries, so cutoff_frequency option should be "forwarded" to every sub match queries.
See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-best-fields.
The best_fields type generates a match query for each field and wraps
them in a dis_max query, to find the single best matching field.
I have a database with entries like
title: This is my awesome title
abstract: A more detailed descriptions of what [...]
I would like to build an Elasticsearch query that matches the above document with, e.g.,
awe detai
In words: A multi_match phrase_prefix query with multiple search terms. (This is intended to be used as a search-as-you-type feature.)
I see how you can combine multi_match and phrase_prefix, but it's unclear to me how to do this for multiple search terms.
Any hints?
Well there is few ways to do that
POST stack/autocomplete/1
{
"title": "This is my awesome title",
"abstract": "A more detailed descriptions of what"
}
Then you can search using query string with star but problem here is that you need to append asterix to query
POST stack/autocomplete/_search
{
"query": {
"query_string": {
"fields": [
"title",
"abstract"
],
"query": "awe* detai*"
}
}
}
If you want to match on user query then you can use like that
POST stack/autocomplete/_search
{
"query": {
"multi_match": {
"fields": [
"title",
"abstract"
],
"query": "awesome tit",
"type": "phrase_prefix"
}
}
}
One more option to consider would be to use nGram with query string so you will not need to modify user query "awe* detai*"
I am attempting to do a query where I filter on term for a specific term. This is the query I am attempting to run:
{
"query": {
"filtered": {
"filter": {
"term": {
"tags": "sports"
}
}
}
},
"sort": {
"timestamp": "desc"
}
}
When I run the same query with a different field (ex: "type": "blog_post") it works, so I am confident in the syntax.
I checked to make sure that tags was properly mapped (I checked at "http://server_name/index/_mapping") and it was.
I also checked that there are documents with "tags" : "sports" in Elasticsearch.
Any ideas what the issue could be? It is only that field, all others work, and "tags" is indexed.
What is the mapping/analyzer you have defined for the field "tags"? If you have not defined any analyzer then it will be analysed using the standard analyzer which in turn will give stemmed token "sport" instead of "sports"
If you do a term search or term filter the input is not analyzed, and will try to search for an exact match. So search for term "sports" won't match.
You should either change the mapping for tags to "not_analyzed" or change the search query to something other than term, like query string query.
Based on a use case you've described I assume tags is mapped as an array of values. That said, term filter can only be used for exact matches.
What I would try is to use terms filter or exist filter instead and change the query to this:
"terms" : { "tags" : "sports" }
or this
"exists" : { "tags" : "sports" }