elasticsearchmatch-phrase

elasticsearch phrase_prefix search


I'm using query_string for full text search and using type to define how to behave with full_text search, one of types that I have to use is phrase_prefix, to return documents that have exact term...

here is my problem:

when I want to search for one word terms.. such as tea the most documents that returns is because of teacher, I know for resolving this issue I have to use phrase type... but when I use this type for one word terms, I reach to another issue, for example ui.. because the most of documents consists UI/UX word, in phrase type search these docs will not return..

so I have a query that must behave like a phrase_prefix but not all the times... and the problem is I don't know the exact times!

if anyone have any solution for my problem.. I'll be so thankful to share that with me.


Solution

  • One simple way to solve this problem would be to use both queries connecting them together with OR in boolean should query. In this case, you would retrieve results in both cases. This means, that's still when you will search for tea you would get both teacher and tea, but since you would have for tea both clauses being matched - you should get higher score for tea. Same would work for UI

    Example of the query would be something like:

    {
      "query": {
        "bool" : {
          "should" : [
            {
              ##query1
            },
            { 
              ##query2
            }
          ]
        }
      }
    }
    

    That of course isn't ideal, but at least it would make you going.

    More information about should clauses - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html