# 簡単なサーチ！

**URL:** <https://discuss.elastic.co/t/topic/87892>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [June 1, 2017, 11:33am UTC](https://discuss.elastic.co/t/topic/87892 "2017-06-01T11:33:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 1, 2017, 11:33am UTC](https://discuss.elastic.co/t/topic/87892/1 "2017-06-01T11:33:47Z")

</div>

すみません、  
私の日本語は下手です、でも日本語の質問です。  
documents: {  
"title": "あいうえお",  
"description": "...スポーツ。2016年リオオリンピックが終了しました。..."  
}  
keyword: 2016年  
バージョン: 5.2.2  
mapping: Kuromoji\_tokenizerを使います。

query:

> ...  
> 'query\_string': {  
> 'fields': [  
> 'title^10',  
> 'title._^4',  
> 'description^2',  
> 'description._^2'  
> ],  
> 'query': u'2016年\*',  
> 'default\_operator': 'AND',  
> 'analyzer': 'keyword',  
> 'fuzziness': 'auto'  
> }  
> ...

これダメです！

英語で、たぶん分かりやすい:  
I have the below description in my document, searching for the keyword: "2016年", but can't hit.  
The Kuromoji tokenizer generate: 2016 and 年 But what the user is looking for is 2016年.  
Same problem if the user search for "50才" or "05月04日".  
How we can this search hit?

手伝ってお願いします！  
m(\_\_)m

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [June 2, 2017, 2:00am UTC](https://discuss.elastic.co/t/topic/87892/2 "2017-06-02T02:00:28Z")

</div>

クエリのanalyzer: "keyword" は正しいですか？

2016年は、 "2016" と "年” の２つのトークンに分かれて格納されます。  
なので、検索時にも検索クエリが ”2016年"という1tokenではなく、"2016"と"年"とならなければヒットしないと思います。  
検索対象のフィールドに設定したanalyzerと異なるanalyzerを検索時に指定していないかどうかが気になりました。

私の環境で試した内容は次の通りですが、検索することができました。

# Mapping Configuration

**analyzer name = "kuromoji"**

```json
PUT forum1
{
  "settings": {
    "index": {
      "number_of_shards": "1",
      "number_of_replicas": "0"
    },
    "analysis": {
      "tokenizer": {
        "kuromoji": {
          "type": "kuromoji_tokenizer",
          "mode": "search"
        }
      },
      "analyzer": {
        "kuromoji": {
          "type": "custom",
          "tokenizer": "kuromoji",
          "filter": [
            "kuromoji_baseform",
            "kuromoji_part_of_speech"
          ]
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "kuromoji",
          "fielddata": true
        },
        "description": {
          "type": "text",
          "analyzer": "kuromoji"
        }
      }
    }
  }
}

```

# Indexing

```json
POST forum1/test/1
{
  "title": "あいうえお",
  "description": "...スポーツ。2016年リオオリンピックが終了しました。..."
}

```

# Search

**using analyzer = kuromoji**

```json
GET forum1/_search
{
  "query": {
    "simple_query_string" : {
      "fields": [
        "title^10",
        "title.*^4",
        "description^2",
        "description.*^2"
        ],
        "query": "2016年",
        "analyzer": "kuromoji",
        "default_operator": "AND"
  }
 }
}

```

and result is below.

```json
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.1395401,
    "hits": [
      {
        "_index": "forum1",
        "_type": "test",
        "_id": "1",
        "_score": 1.1395401,
        "_source": {
          "title": "あいうえお",
          "description": "...スポーツ。2016年リオオリンピックが終了しました。..."
        }
      }
    ]
  }
}

```

if using analyzer = "keyword", no hits.

best regards.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 2, 2017, 2:39am UTC](https://discuss.elastic.co/t/topic/87892/3 "2017-06-02T02:39:43Z")

</div>

@tsgkdt ありがとうございます！

問題はkuromojiのアナライザーを使えれば、"...スポーツ。2015年リオオリンピックが終了しました。..." と "2016-05-05" etc...  
hitsは多すぎです。

おかの例は：  
”16才” ---\> "16第の話、1才から。。。" がhitします。  
だからkeywordアナライザーを使います。

英語で:  
What I want is the full word 2016年 or 16才 only documents with 16 and 才 not all documents with 16 and all documents with 才.  
I hope it's more clear, sorry for the misunderstood and great thanks for the help.

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [June 2, 2017, 2:59am UTC](https://discuss.elastic.co/t/topic/87892/4 "2017-06-02T02:59:07Z")

</div>

クエリを " で囲うのはどうでしょう？

2016年 では、 「2016回目のプロポーズ、平成29年。」は、ヒットしますが  
"2016年"とすることにより、 ”2016"と”年”が連続して出現するところを探すようになります。

please confirm below.

```json
GET forum1/_search
{
  "query": {
    "simple_query_string" : {
      "fields": [
        "title^10",
        "title.*^4",
        "description^2",
        "description.*^2"
        ],
        "query": "\"2016年\"",
        "analyzer": "kuromoji",
        "default_operator": "AND"
  }
 }
}

```

best regards

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 2, 2017, 3:18am UTC](https://discuss.elastic.co/t/topic/87892/5 "2017-06-02T03:18:10Z")

</div>

hehehe " で囲うは忘れました。。。 😓

I'll give a try ありがとうございます。

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [June 2, 2017, 3:26am UTC](https://discuss.elastic.co/t/topic/87892/6 "2017-06-02T03:26:17Z")

</div>

または Phrase Queryも良いかもしれません。  
[https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-match-query-phrase.html#query-dsl-match-query-phrase](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-match-query-phrase.html#query-dsl-match-query-phrase)

example.

```json
GET forum1/_search
{
  "query": {
    "match_phrase": {
      "description": "2016年"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 2, 2017, 6:03am UTC](https://discuss.elastic.co/t/topic/87892/7 "2017-06-02T06:03:45Z")

</div>

Thanks for the help.

The case was more complex and I figure out another solution that didn't broke all my unit test.  
Thanks a lot for the help.

ありがとうございます m(\_\_)m

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 30, 2017, 6:04am UTC](https://discuss.elastic.co/t/topic/87892/8 "2017-06-30T06:04:36Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
