Slad
Slad

Reputation: 17

Elastic search query string

Why can't I get the same result in the second query as in the third one? What am I doing wrong?

  1. I make this query:
{
  "size": 20,
  "track_total_hits": false,
  "_source": [
    "title"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "63 ",
            "default_field": "title",
            "type": "phrase_prefix"
          }
        }
      ]
    }
  }
}

and got this result:

{
  "hits": {
    "max_score": 13.483224,
    "hits": [
      {
        "_index": "products_2022_11_3_17_30_44_56920",
        "_type": "_doc",
        "_id": "19637",
        "_score": 13.483224,
        "_source": {
          "title": "Заднее стекло 6302BGNE"
        }
      }
    ]
  }
}
  1. all right, after this I am typing one more character:

"query": "63 2"

and got empty result:

"hits" : {
    "max_score" : null,
    "hits" : [ ]
  }
}
  1. then I am adding one more character again:

"query": "63 21"

and got not empty result again:

{
  "hits": [
    {
      "_index": "products_2022_11_3_17_30_44_56920",
      "_type": "_doc",
      "_id": "105863",
      "_score": 440.54578,
      "_source": {
        "title": "Лампа накаливания 63 21 0 151 620 BMW"
      }
    }
  ]
}

Index mapping:

"title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },

GET products/_settings

{
  "products_2022_11_7_8_57_7_118045" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "products_2022_11_7_8_57_7_118045",
        "creation_date" : "1667800627119",
        "number_of_replicas" : "0",
        "uuid" : "GV6-5tzQQPavncFUcvq9NA",
        "version" : {
          "created" : "7170299"
        }
      }
    }
  }
}

Upvotes: 1

Views: 112

Answers (1)

Amit
Amit

Reputation: 32386

Looks like you are using the some analyzer on your title field, that is creating tokens in search a way it doesn't match your search term.

I used the standard analyzer for title field and index the sample documents shown by you and its giving me results in all three queries. as shown below:

{
    "size": 20,
    "track_total_hits": true,
    "_source": [
        "title"
    ],
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "63 2",
                        "default_field": "title",
                        "type": "phrase_prefix"
                    }
                }
            ]
        }
    }
}

Search Result

 "hits": [
            {
                "_index": "74308224",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.1689311,
                "_source": {
                    "title": "Лампа накаливания 63 21 0 151 620 BMW"
                }
            }
        ]

Giving your index mapping and settings would be helpful to identify why its not giving the expected result.

Upvotes: 1

Related Questions