ceth
ceth

Reputation: 45295

Match by substring

I need to get the documents where Description field contains substring 28/859. Here is my query:

{
  "explain": true,
  "query": {
    "bool":{
      "filter":{
        "bool":{"should":[{"query_string":{"default_field":"Description","query":"28//859", 
                                           "analyzer": "keyword"}}]}},
      "must_not":{"exists":{"field":"ParentId"}}}
  }
}

I get the documents with:

How can I get the documents with substring 28/859 only ?

Update: explain example:

{
        "_shard": "[tech_places][4]",
        "_node": "u7QI_gjjRXy4-xdqnK2KMw",
        "_index": "tech_places",
        "_type": "entity",
        "_id": "8403",
        "_score": 0.0,
        "_source": {
          "Id": 8403,
          "Name": "RETE-43424",
          "Description": "SRF-10kv №28 VISO",
          "ParentId": null,
          "OrganizationId": 12,
          "OrganizationPath": "12_27",
          "LocationId": 27,
          "Classification": "",
          "Type": "A",
          "Status": 0,
          "MaintenanceObjectId": null,
          "TreePath": "8403"
        },
        "_explanation": {
          "value": 0.0,
          "description": "sum of:",
          "details": [
            {
              "value": 0.0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0.0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 0.0,
                  "description": "sum of:",
                  "details": [
                    {
                      "value": 0.0,
                      "description": "weight(Description:28 in 35112) [], result of:",
                      "details": [
                        {
                          "value": 0.0,
                          "description": "score(doc=35112,freq=1.0), with freq of:",
                          "details": [
                            {
                              "value": 1.0,
                              "description": "termFreq=1.0",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      },

Upvotes: 0

Views: 148

Answers (2)

Ankit
Ankit

Reputation: 730

Match_Phrase should serve the requirement.

Sample Code :

GET <indexname>/_search
{

  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "Description": """28/859"""
          }
        }
      ]
    }
  }
}

Upvotes: 0

Nishant
Nishant

Reputation: 7864

You can use whitespace analyzer for this. Create/Update your mapping with description property as below:

{
  "description": {
    "type": "text",
    "analyzer": "whitespace"
  }
}

This will make sure that something like 28/859 is treated as s single token. You can even created your own custom tokenizer/analyzer using regex. Then you can use the query below to get required result:

{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "28\\/859"
    }
  }
}

Upvotes: 1

Related Questions