Elastic Search: query_string query does not match exact phrase in full text search - elasticsearch

I am using Elastic search 6.2.3. We are using the query_string full-text-query for the full-text search. At present, if we search lazy brown fox it searches any file that has all these words lazy, brown and fox but it does not look for exact-phrase 'lazy brown fox', even default slop is zero.
Here is an example:
{
"query": {
"query_string": {
"fields": [],
"query": "lazy AND brown AND fox"
}
}
}
I have looked at the match-phrase-query but the issue is that we have to specify the field name(s) in match-phrase-query whereas, in string-query, it's working with a blank array at fields option.
Please suggest, how to get the exact phrase match results using query_string full text-query?

Instead of looking at match-phrase query to run phrase query on multiple fields take a look at multi_match which do supports phrase type query
POST phrase_index/_search
{
"query": {
"multi_match": {
"query": "this is where it should work",
"fields": [],
"type": "phrase"
}
}
}

Related

Keyword match(Exact Match) in Elasticsearch ignoring the order of keywords

I am working on a use case where in I need only exact match but the words can be in any order.
Example :-
Searching with Text :- Quick Brown Fox
Should Match with :-
- Quick Brown Fox (Can Achieve with analyzer Keyword)
- Brown Quick Fox
- Fox Quick Brown
Shouldn't Match with :-
- Brown Quick Fox Rocky
- My Brown Quick Fox Rocky
If i try to use match with slop 0, the issue is I am also getting matches with Brown quick Fox Rocky which is not desirable. So i am looking for a analyzer which can be used with keyword but can handle the order of word. Till now no success achieved any ideas from the community is most welcome.
Thanks in advance !!!!
Not very ideal, but I think it gets the job done.
{
"mappings": {
"properties": {
"text": {
"type": "text",
"analyzer": "whitespace",
"fields": {
"length": {
"type": "token_count",
"analyzer": "whitespace"
}
}
}
}
}
}
Key elements here:
whitespace analyzer (because this is what I see in your test data; if you have some other rules, you need to change this)
token_count type of sub-field that, at indexing time, indexes the number of tokens from that specific field
Then, at search time, the idea is for your text to search to match all the terms and, also, to have the length of tokens equal to the one indexed. Not ideal, I said, because the number of token in the searched text needs to be computed before running the query and placed inside the query. If a simple analyzer is used - like the whitespace one - you can achieve this outside Elasticsearch with a simple tokenizer of some sort (depending on the application/language you are using) and compute the number of tokens.
And the query:
{
"query": {
"bool": {
"must": [
{
"match": {
"text": {
"query":"Quick Brown Fox",
"operator": "and"
}
}
},
{
"term": {
"text.length": 3
}
}
]
}
}
}
As I said, the 3 you see there needs to be computed outside Elasticsearch by "looking" at the searched text.

ANDing search keywords for elastic Search

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"
}
}
}

Elasticsearch doesn't return results on a multi match query

I'm wondering why Elasticsearch doesn't give me any results for the following Multi Match Query:
GET /stag/_search
{
"query": {
"multi_match": {
"type": "phrase_prefix",
"query": "ferran ma",
"fields": [ "fullName", "fullName.folded" ]
}
}
}
But it gives me results on:
GET /stag/_search
{
"query": {
"multi_match": {
"type": "phrase_prefix",
"query": "ferran may",
"fields": [ "fullName", "fullName.folded" ]
}
}
}
I thought that maybe there is a minimum character length per word but then I've seen the following query:
GET /stag/_search
{
"query": {
"multi_match": {
"type": "phrase_prefix",
"query": "ignasi t",
"fields": [ "fullName", "fullName.folded" ]
}
}
}
Is giving me results. So I have no idea what's going on.
Seems like the problem is explained here
The match_phrase_prefix query is a poor-man’s autocomplete. It is very
easy to use, which lets you get started quickly with
search-as-you-type but its results, which usually are good enough, can
sometimes be confusing.
Consider the query string quick brown f. This query works by creating
a phrase query out of quick and brown (i.e. the term quick must exist
and must be followed by the term brown). Then it looks at the sorted
term dictionary to find the first 50 terms that begin with f, and adds
these terms to the phrase query.
The problem is that the first 50 terms may not include the term fox so
the phrase quick brown fox will not be found. This usually isn’t a
problem as the user will continue to type more letters until the word
they are looking for appears.

Punctuation with wildcard search in Elasticsearch

I have a custom analyzer on field authlast that replaces punctuation with space. So when search with saint-, I am able to get results, but when I search with saint-* I get no results. Any idea why?
How does query_string analyze the string before submitting it for the search? If it does not analyze how does the term looks like when query_string submits the term to the ES index?
$"query": {
{
"query_string": {
"query": "saint-*",
"fields": ["authlast"],
"default_operator": "AND"
}
}
}
window.jQuery.

Elasticsearch: multi_match phrase_prefix query with multiple search terms

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*"

Resources