Query string returns 0 hits


(Mohammed Essabri) #1

Hi there,

I am trying to add exact matching functionality in my app, but I found some issues.

I created an index exactmatching_v4 in my Elasticsearch cluster ( version 1.7 ) with following mappings:

POST /exactmatching_v4    
    {
      "aliases": {},
      "mappings": {
        
        "document": {
          
          "properties": {
            "content": {
              "type": "string",
              "index": "not_analyzed"
            },
            "lang": {
              "type": "string",
              "index": "not_analyzed"
            },
            "path": {
              "type": "string"
            }
          }
        }
      }
    }

And I added the following documents

PUT /exactmatching/document/1
{
  "content": "do test",
  "lang": "en",
  "path": "/1"
} 

 PUT /exactmatching/document/2
    {
      "content": "the test has been done",
      "lang": "en",
      "path": "/2"
    }

 PUT /exactmatching/document/3
        {
          "content": "give me some tests",
          "lang": "en",
          "path": "/3"
        }

 PUT /exactmatching/document/4
            {
              "content": "different one",
              "lang": "en",
              "path": "/4"
            }

The goal is to make a search that will find to me the docs containing the exact sentence some tests

At the first time, I had to find all docs containing the word test using query_string, but it doesn't give any thing.

{
  "query": {
    "query_string": {
      "fields": [
        "content"
      ],
      "query": "test",
      "analyze_wildcard": true
    }
  }
}

Please not that I added "analyze_wildcard": true by following a solution proposed in the following issue.

Thank you all.


(Henning Andersen) #2

Hi @mohammedEssabri,

the content field is defined as not_analyzed and therefore only supports exact match. I believe you will need to let the field be analyzed to search for terms in it.

In order to find a specific phrase like some tests, you will likely need to use a phrase query, either by quoting the text you search for using query_string or using match_phrase.


(Mohammed Essabri) #3

Hi @HenningAndersen,

Yes, I'm trying to find the exact matching. I mean to find doc containing the exact phrase.

You're right, I had to use the quotes.

Hereafter is the new mapping

"document": {
    "properties": {
      "path": {
        "type": "string"
      },
      "lang": {
        "index": "not_analyzed",
        "type": "string"
      },
      "content": {
        "index": "not_analyzed",
        "type": "string"
      }
    }
  }

I removed the "not-analyzed" option.

The search query is as following:

{
  "query": {
    "query_string": {
      "fields": [
        "content"
      ],
      "query": "\"do thi\"",
      "analyze_wildcard": true
    }
  }
}

Thank you again :slight_smile: