Search code examples
javaelasticsearchelasticsearch-7

search in fields containing apostrophe does not return results elasticsearch


my search on a field containing apostrophe with elasticsearch not returning results. example of field : Objet de l'opération de crédit here is my query:

{
    "size": 100,
    "query": {
        "bool": {
            "must": [{
                    "match_phrase": {
                        "Objet de l'opération de crédit": {
                            "query": "SMD : Investissement locatif",
                            "slop": 0,
                            "zero_terms_query": "NONE",
                            "boost": 1.0
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    }
}

this query does not return results and only for fields with apostrophe help please i am new to elasticsearch


Solution

  • TL;DR: The problem was not the apostrophe on the field name but at space at the end of the field name.

    Elasticsearch will not warn about non existent fields on typos on the query, will just return no results.

    Old answer

    It would be helpful if you add the example document you want to return with that query, because using the same value in a test doc in fact the query you posted return results.

    Without that information I can guess the document is not being found because you are using "match_phrase" that means to match the entire content as a phrase and slop 0 that means you are not allowing words in between, or different words order.

    I would suggest to try with a less strict query first.

    POST test_alma/_search
    {
      "query": {
        "match": {
          "Objet de l'opération de crédit": "SMD : Investissement locatif"
        }
      }
    }
    

    Ingesting document

    POST test_alma/_doc
    {
       "Objet de l'opération de crédit": "SMD : Investissement locatif"
    }
    

    Your Query

    POST test_alma/_search
    {
      "size": 100,
      "query": {
        "bool": {
          "must": [
            {
              "match_phrase": {
                "Objet de l'opération de crédit": {
                  "query": "SMD : Investissement locatif",
                  "slop": 0,
                  "zero_terms_query": "NONE",
                  "boost": 1
                }
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      }
    }
    

    Response

    {
      "took" : 0,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.8630463,
        "hits" : [
          {
            "_index" : "test_alma",
            "_type" : "_doc",
            "_id" : "asod-ncBRP0FeAG5QeOY",
            "_score" : 0.8630463,
            "_source" : {
              "Objet de l'opération de crédit" : "SMD : Investissement locatif"
            }
          }
        ]
      }
    }