elasticsearch返回加工字数

时间:2016-07-08 16:04:14

标签: elasticsearch

我有一个索引如下

<g>
    <g>
        <path d="M311.517,14.053c-99.265,0-180.024,80.66-180.024,179.805c0,41.538,14.182,79.827,37.955,110.303L0,451.176l22.889,26.313    l170.577-147.994c31.628,27.496,72.926,44.168,118.051,44.168c99.266,0,180.025-80.661,180.025-179.805    C491.542,94.713,410.784,14.053,311.517,14.053z M311.517,353.663c-88.237,0-160.024-71.688-160.024-159.805    S223.279,34.053,311.517,34.053s160.025,71.688,160.025,159.805S399.755,353.663,311.517,353.663z" fill="#FFFFFF"/>
        <polygon points="322.447,122.812 300.587,122.812 300.587,182.928 240.471,182.928 240.471,204.788 300.587,204.788     300.587,264.904 322.447,264.904 322.447,204.788 382.563,204.788 382.563,182.928 322.447,182.928   " fill="#000000"/>
    </g>
</g>

我有一个要搜索的文字:

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

我想要实现的是在我的索引中找到上面短语中有多少确切的独特单词,cID = 2

预期结果: 5 (我的,文字,是,随机,a)

有任何想法使用elasticsearch吗?

1 个答案:

答案 0 :(得分:2)

这应该是可能的。首先,使用所有单词OR一起运行搜索,然后使用聚合来确定表示哪些单词。获得对查询的响应后,您需要将点击数加起来。

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,
}'