Lakshman Diwaakar
Lakshman Diwaakar

Reputation: 7589

What is the query required for fetching full-text with delimiter in elasticsearch

Assuming I have a document like this in elasticSearch :

{
  "videoName": "taylor.mp4",
  "type": "long"
}

I tried full-text search using the DSL query:

{
    "query": {
        "match":{
            "videoName": "taylor"
        }
    }
}

I need to get the above document, but I don't get it .If I specify taylor.mp4, it returns the document.

So, I would like to know, how to make full-text search with delimiters.

Edit after KARTHEEK answer:

The regexp fetches the taylor.mp4 document. Take the situation, where the document in video index are:

{
  "videoName": "Akon - smack that.mp4",
  "type": "long"
}

So, the query for retrieving this document can be ,

{
    "query": {
        "match":{
            "videoName": "smack that"
        }
    }
}

In this case, the document will be retrieved, since we use smack in the query string. match does the full-text search and gets us the document. But, say I only know the that keyword and the match, doesn't get the document. I need to use regexp for that.

{
    "query": {
        "regexp":{
            "videoName": "smack.* that.*"
        }
    }
}

On the Other hand, if i take up regexp and make all my query strings to smack.* that.*, this will also not retrieve any documents. And, we dont know which word will have its suffix .mp4. So, my question is we need to do the full-text search with match, and it should also detect the delimiters. Is there any other way ?

Edit after Richa asked the mapping of index

for http://localhost:9200/example/videos/_mapping

{
  "example": {
    "mappings": {
      "videos": {
        "properties": {
          "query": {
            "properties": {
              "match": {
                "properties": {
                  "videoName": {
                    "type": "string"
                  }
                }
              }
            }
          },
          "type": {
            "type": "string"
          },
          "videoName": {
            "type": "string"
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 186

Answers (2)

KARTHEEK GUMMALURI
KARTHEEK GUMMALURI

Reputation: 345

Please use this mapping

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "videoName": {
               "type": "string",
               "term_vector": "yes"
            }
         }
      }
   }
}

After that you need to index a document that you mentioned earlier:

PUT test_index/doc/1
{
  "videoName": "Akon - smack that.mp4",
  "type": "long"
}

Output:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.15342641,
    "hits": [
      {
        "_index": "test_index",
        "_type": "doc",
        "_id": "1",
        "_score": 0.15342641,
        "_source": {
          "videoName": "Akon - smack that.mp4",
          "type": "long"
        }
      }
    ]
  }
}

Query to get results:

GET /test_index/doc/1/_termvector?fields=videoName

Results:

{
  "_index": "test_index",
  "_type": "doc",
  "_id": "1",
  "_version": 1,
  "found": true,
  "took": 1,
  "term_vectors": {
    "videoName": {
      "field_statistics": {
        "sum_doc_freq": 3,
        "doc_count": 1,
        "sum_ttf": 3
      },
      "terms": {
        "akon": {
          "term_freq": 1
        },
        "smack": {
          "term_freq": 1
        },
        "that.mp4": {
          "term_freq": 1
        }
      }
    }
  }
}

By using this we will search based on "smack"

POST /test_index/_search
{
    "query": {
        "match": {
           "_all": "smack"
        }
    }
}

Result:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.15342641,
    "hits": [
      {
        "_index": "test_index",
        "_type": "doc",
        "_id": "1",
        "_score": 0.15342641,
        "_source": {
          "videoName": "Akon - smack that.mp4",
          "type": "long"
        }
      }
    ]
  }
}

Upvotes: 2

KARTHEEK GUMMALURI
KARTHEEK GUMMALURI

Reputation: 345

Depending upon above query you mentioned right we can use regular expression in order get the result.Please find attached result for your perusal and let me know if there are anything else you want.

curl -XGET "http://localhost:9200/test/sample/_search" -d'
{
  "query": { 
    "regexp":{
        "videoName": "taylor.*"
    }
  }
}'

Result:

{
  "took": 22,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "sample",
        "_id": "1",
        "_score": 1,
        "_source": {
          "videoName": "taylor.mp4",
          "type": "long"
        }
      }
    ]
  }
}

Upvotes: 2

Related Questions