1

I have a requirement to be able to search a sentence as complete or with prefix. The UI library (reactive search) I am using is generating the query in this way:

"simple_query_string": {
  "query": "\"Louis George Maurice Adolphe\"",
  "fields": [
    "field1",
    "field2",    
    "field3"
  ],
  "default_operator": "or"
}

I am expecting it to returns results for eg. Louis George Maurice Adolphe (Roche) but NOT just records containing partial terms like Louis or George

Currently, I have code like this but it only brings the record if I search with complete word Louis George Maurice Adolphe (Roche) but not a prefix Louis George Maurice Adolphe.

{
  "settings": {
    "analysis": {
      "char_filter": {
        "space_remover": {
          "type": "mapping",
          "mappings": [
            "\\u0020=>"
          ]
        }
      },
      "normalizer": {
        "lower_case_normalizer": {
          "type": "custom",
          "char_filter": [
            "space_remover"
          ],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "field3": {
          "type": "keyword",
          "normalizer": "lower_case_normalizer"
        }
      }
    }
  }
}

Any guidance on the above is appreciated. Thanks.

sunny
  • 49
  • 1
  • 1
  • 3

2 Answers2

0

You are not using the prefix query hence not getting result for prefix search terms, I used same mapping and sample doc, but changed the search query which gives the expected results

Index mapping

{
    "settings": {
        "analysis": {
            "char_filter": {
                "space_remover": {
                    "type": "mapping",
                    "mappings": [
                        "\\u0020=>"
                    ]
                }
            },
            "normalizer": {
                "lower_case_normalizer": {
                    "type": "custom",
                    "char_filter": [
                        "space_remover"
                    ],
                    "filter": [
                        "lowercase"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "field3": {
                "type": "keyword",
                "normalizer": "lower_case_normalizer"
            }
        }
    }
}

Indexed sample doc

{
   "field3" : "Louis George Maurice Adolphe (Roche)"
}

Search query

{
  "query": {
    "prefix": {
     "field3": {
        "value": "Louis George Maurice Adolphe"
      }
    }
  }
}

Search result

"hits": [
            {
                "_index": "normal",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "field3": "Louis George Maurice Adolphe (Roche)"
                }
            }
        ]

Amit
  • 30,756
  • 6
  • 57
  • 88
  • Thanks for the response. The issue here is the user queries it through a search box and the library generates the `simple_query_string`. The user's search term can match any of the fields field1, field2, field3 or field4. How can I change the query by still maintaining this functionality? And all the fields can be linked to different analyzers. – sunny Sep 15 '20 at 15:03
  • adding mutilple fields in my search query is not a big deal but def. you have to change the logic in your library to create the prefix query than simple_q_string – Amit Sep 15 '20 at 15:07
  • @sunny any luck and update? please let me know if you have follow up questions – Amit Nov 19 '20 at 17:05
0

The underlying issue stems from the fact that you're applying a whitespace remover. What this practically means is that when you ingest your docs:

GET your_index_name/_analyze
{
  "text": "Louis George Maurice Adolphe (Roche)",
  "field": "field3"
}

they're indexed as

{
  "tokens" : [
    {
      "token" : "louisgeorgemauriceadolphe(roche)",
      "start_offset" : 0,
      "end_offset" : 36,
      "type" : "word",
      "position" : 0
    }
  ]
}

So if you indend to use simple_string, you may want to rethink your normalizers.

@Ninja's answer fails when you search for George Maurice Adolphe, i.e. no prefix intersection.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
  • yes, I found his token as he mentioned he needs the prefix search so added that solution, if he needs more comibination which we don't know atm, than obviously it requires more fine tunning – Amit Sep 15 '20 at 15:02