0

Say I have an analyzed field, and I want to do a search that matches on that field if the field starts with the supplied phrase. For example, say I have two documents, each with a single field:

doc1 : {
    name: "the dog is happy"
}

doc2: {
    name: "happy the dog is"
}

Say my query string is "the dog is". I want to match doc1 and not doc2. How can I do this?

Thanks!

PeteyPabPro
  • 839
  • 10
  • 18
  • If you find all those documents which start with `the dog is`. Will that suffice your requirement. – Richa Apr 07 '16 at 03:57
  • @richa yes, that's exactly what I'd like. – PeteyPabPro Apr 07 '16 at 03:59
  • if you change your field to `not_analyzed` , your problem will get solved.. – Richa Apr 07 '16 at 04:06
  • @richa yes, I've considered this, but it seems odd that this would be necessary since the inverted index would have all the information necessary even if this field were analyzed (namely, all matching phrases with the first word in position 1) – PeteyPabPro Apr 07 '16 at 04:10

2 Answers2

4

If you mark the beginning of each sentence in your documents with a special token, you can do a phrase matching query:

So if you index your documents like that

doc1 : {
    name: "START the dog is happy"
}

doc2: {
    name: "START happy the dog is"
}

You get the desired result with this query:

POST /test/test/_search
{
  "query": {
    "match_phrase": {
      "name": "START the dog is"
    }
  }
}
  • https://discuss.elastic.co/t/how-do-i-get-elasticsearch-max-clause-count-to-take-effect/6133/9 Using phrase prefix in production can very painful, and inefficient.. It not only effects the performance..it also results in incorrect results.. And throws error which is mentioned in the above discussion..which exactly tells why not use phrase prefix.. http://stackoverflow.com/questions/20105466/elasticsearch-search-perfomance You can also refer to this question on how phrase prefix works which leads to the error – Anirudh Modi Apr 07 '16 at 05:01
  • Sorry, but I didn't proposed to use phrase prefix?! – Michael Stockerl Apr 07 '16 at 05:08
  • No problem, it would be nice if you would also undo your downvote. – Michael Stockerl Apr 07 '16 at 05:19
  • I can;t do, if edit your answer only then I would be able to undo... So if you can make an edit i will undo my changes – Anirudh Modi Apr 07 '16 at 05:28
  • This still allows for inexact prefix matches where junk records make it to the top of the pile. – Dissident Rage Jun 01 '20 at 13:03
  • this is clever. – aaa90210 Apr 09 '21 at 04:50
-1
GET /_search
{
    "query": {
        "prefix": {
            "user": {
                "value": "the dog is"
            }
        }
    }
}

Also,
To Speed up prefix queries You can speed up prefix queries using the index_prefixes mapping parameter. If enabled, Elasticsearch indexes prefixes between 2 and 5 characters in a separate field. This lets Elasticsearch run prefix queries more efficiently at the cost of a larger index.

Kaushik J
  • 962
  • 7
  • 17