0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

10rep - フレーズ検索 Elasticsearch

Posted at

正確にマッチさせたいフレーズ検索

  • フレーズで簡単に検索するには、次の例のようにダブルクォーテーションで単語を囲みます。
  • explain 有効にしていれば、フレーズでマッチしていることが確認できます
GET one_year_comment/_search
{
  "query": {
    "query_string":{
      "default_field" : "comment",
      "query":"\"{フレーズ}\""
    }
  },
  "size": 100
}

複数

GET one_year_comment/_search
{
  "query": {
    "query_string":{
      "default_field" : "comment",
      "query":"\"{フレーズ}\" \"{フレーズ}\""
    }
  },
  "size": 100
}

Python

import pandas as pd
from elasticsearch import Elasticsearch

fname="FILE_NAME.tsv"
df_tsv = pd.read_csv(fname, sep='\t', dtype=str)

es = Elasticsearch("http://{IP}:9200")

search_list = df_tsv['検索対象'].tolist()
search_count_res = []

##############################
for child in search_list:
  es_count_res = es.count(
                   index="one_year_comment",
                   body={
                          "query": {
                            "query_string": {
                              "default_field" : "comment",
                              "query":'\"'+child+'\"'
                            }
                         }
 })
  search_count_res.append(es_count_res["count"])
##############################

df_tsv['search_count'] = search_count_res

#print( df_tsv[["検索対象","search_count"]]  )
df_tsv.to_csv('OUTPUT_SERACH_COMMENT.tsv', sep='\t')
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?