user2733521
user2733521

Reputation: 449

elasticsearch return number of maching words

I have an index as follow

IDX  text                          cID
#1 - "this is a random text"      - 2
#2 - "another random cool test"   - 3
#3 - "my car is blue       "      - 2
#4 - "lorem ipsum indolor si"     - 3
#5 - "i don't know what is it for"- 2

I have a text to search against :

My very cool text is a long text with many words and random sense.

What i'm trying to achieve is to find how many of exact unique words in the phrase above i have in my index with cID = 2

Expected results : 5 (my, text, is, random, a)

Any idea to do this with elasticsearch ?

Upvotes: 2

Views: 101

Answers (1)

speedplane
speedplane

Reputation: 16121

This should be possible. First, run the search with all of the words ORed together, and then use aggregations to determine which words are represented. After getting the response to the query, you'll need to add up the number of aggregations with hits.

curl -XGET localhost:9200/dockets/_search?pretty -d '
{
  "query": {
        "bool" : {
            "minimum_should_match" : 1,
              "should": [
                  { "term": { "_all": "my"} },
                  { "term": { "_all": "very"} },
                  { "term": { "_all": "cool"} }
            ]
        }
  },
  "aggs" : {
    "agg_my" : { "terms": { "_all": "my", "size":1, "shard_size":1} },
    "agg_very" : { "terms": { "_all": "very", "size":1, "shard_size":1} },
    "agg_cool" : { "terms": { "_all": "cool", "size":1, "shard_size":1} }
  },
  "size": 0,
  "from": 0,
}'

Upvotes: 2

Related Questions