簡體   English   中英

獲取特定術語在彈性搜索字段中的出現次數

[英]Get the number of appearances of a particular term in an elasticsearch field

我有一個具有以下映射的彈性搜索索引(帖子):

{
    "id": "integer",
    "title": "text",
    "description": "text"
}

我只想在單個特定文檔的描述字段中找到特定術語的出現次數(我有文檔 ID 和要查找的術語)。

例如,我有一個像這樣的帖子 {id: 123, title:"some title", description: "my city is LA, this post description has twooccurrence of word city"}。

我有這個帖子的文檔 ID/帖子 ID,只是想找出這個特定帖子的描述中出現了多少次“城市”這個詞。 (在這種情況下,結果應該是 2)

似乎無法找到此搜索的方法,我不希望出現在所有文檔中,而只希望出現在單個文檔及其“一個字段內”。 請為此提出一個查詢。 謝謝

彈性搜索版本:7.5

您可以在description使用terms聚合,但需要確保其fielddata設置為true

PUT kamboh/
{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "title": {
        "type": "text"
      },
      "description": {
        "type": "text",
        "fields": {
          "simple_analyzer": {
            "type": "text",
            "fielddata": true,
            "analyzer": "simple"
          },
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

攝取示例文檔:

PUT kamboh/_doc/1
{
  "id": 123,
  "title": "some title",
  "description": "my city is LA, this post description has two occurrences of word city "
}

聚合:

GET kamboh/_search
{
  "size": 0,
  "aggregations": {
    "terms_agg": {
      "terms": {
        "field": "description.simple_analyzer",
        "size": 20
      }
    }
  }
}

產量:

"aggregations" : {
    "terms_agg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "city",
          "doc_count" : 1
        },
        {
          "key" : "description",
          "doc_count" : 1
        },
        ...
      ]
    }
  }

現在,如您所見, simple分析器將字符串拆分為單詞並使它們小寫,但它也擺脫了字符串中的重復城市! 我無法想出一個可以保留重復項的分析器......話雖如此,

建議在索引之前進行這些字數統計!

您可以按空格拆分字符串並將它們索引為單詞數組而不是長字符串。


這在搜索時也是可能的,盡管它非常昂貴,不能很好地擴展並且你需要在你的 es.yaml 中有script.painless.regex.enabled: true

GET kamboh/_search
{
  "size": 0,
  "aggregations": {
    "terms_script": {
      "scripted_metric": {
        "params": {
          "word_of_interest": ""
        },
        "init_script": "state.map = [:];",
        "map_script": """
              if (!doc.containsKey('description')) return;

              def split_by_whitespace = / /.split(doc['description.keyword'].value);

              for (def word : split_by_whitespace) {  
                 if (params['word_of_interest'] !== "" && params['word_of_interest'] != word) {
                   return;
                 } 

                 if (state.map.containsKey(word)) {
                   state.map[word] += 1;
                   return;
                 }

                 state.map[word] = 1;
              }
""",
        "combine_script": "return state.map;",
        "reduce_script": "return states;"
      }
    }
  }
}

屈服

...
"aggregations" : {
    "terms_script" : {
      "value" : [
        {
          "occurrences" : 1,
          "post" : 1,
          "city" : 2,  <------
          "LA," : 1,
          "of" : 1,
          "this" : 1,
          "description" : 1,
          "is" : 1,
          "has" : 1,
          "my" : 1,
          "two" : 1,
          "word" : 1
        }
      ]
    }
  }
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM