# Is there some wrong usage of my query json?

**URL:** <https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736>\
**Category:** Elasticsearch\
**Created:** [December 1, 2016, 10:01am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736 "2016-12-01T10:01:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![beenoisy](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@beenoisy](https://discuss.elastic.co/u/beenoisy)\
**Post date:** [December 1, 2016, 10:01am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/1 "2016-12-01T10:01:09Z")

</div>

Here is my es settings:

```auto
// http://10.94.107.21:9200/ugc

{
  "ugc": {
    "aliases": {
      
    },
    "mappings": {
      "article": {
        "_all": {
          "store": true,
          "analyzer": "ik_smart"
        },
        "properties": {
          "authorDept1": {
            "type": "string"
          },
          "authorDept2": {
            "type": "string"
          },
          "authorDept3": {
            "type": "string"
          },
          "authorDept4": {
            "type": "string"
          },
          "authorDept5": {
            "type": "string"
          },
          "authorDisplayName": {
            "type": "string"
          },
          "authorDisplayNamePinYin": {
            "type": "string"
          },
          "authorEmail": {
            "type": "string"
          },
          "authorEmployeeId": {
            "type": "string"
          },
          "authorId": {
            "type": "long"
          },
          "authorUsername": {
            "type": "string"
          },
          "content": {
            "type": "string",
            "boost": 4.0,
            "analyzer": "ik_smart",
            "include_in_all": true
          },
          "id": {
            "type": "long"
          },
          "publishTime": {
            "type": "string"
          },
          "tags": {
            "type": "string",
            "boost": 10.0,
            "analyzer": "ik_smart",
            "include_in_all": true
          },
          "title": {
            "type": "string",
            "boost": 10.0,
            "analyzer": "ik_smart",
            "include_in_all": true
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1480584398456",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "ly7I_JPFQZSX5xRL1XOGow",
        "version": {
          "created": "2040199"
        }
      }
    },
    "warmers": {
      
    }
  }
}

```

`curl http://10.94.107.21:9200/ugc/article/_search`

response string:

```auto

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 47,
    "max_score": 1.0,
    "hits": [
      {
        "_index": "ugc",
        "_type": "article",
        "_id": "3858",
        "_score": 1.0,
        "_source": {
          "id": 3858,
          "title": "漫谈TCP",
          "content": "content of this document",
          "publishTime": "Nov 9, 2016 6:10:14 PM",
          "tags": [
            "tcp"
          ],
          "authorId": 5768,
          "authorUsername": "yejianfeng",
          "authorDisplayName": "叶剑峰",
          "authorDisplayNamePinYin": [
            "yejianfeng",
            "xiejianfeng"
          ],
          "authorEmail": "yejianfeng@bing.com",
          "authorEmployeeId": "05616",
          "authorDept1": "CEO",
          "authorDept2": "工程生产力部",
          "authorDept3": "运力中心",
          "authorDept4": "产品技术中心",
          "authorDept5": "运力中心技术部"
        }
      },
      {
        "_index": "ugc",
        "_type": "article",
        "_id": "3852",
        "_score": 1.0,
        "_source": {
          "id": 3852,
          "title": ""
.......

```

And, my query json is:

```auto

{
   "query": {
       "filtered":{
           "filter":{
               "bool":{
                   "must":{
                       "bool": {
                           "must":{"term":{"authorDept2": "工程生产力部"}}
                       }
                   },
                   "must":{
                       "bool": {
                           
                       }
                   },
                   "must":{
                       "bool": {
                           
                       }
                   }
               }
           }
       }
   }
}

```

the query response:

```auto

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

```

Is there some wrong usage of my query string?  
I hope I can get document from es by filter only.

Thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 1, 2016, 12:46pm UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/2 "2016-12-01T12:46:22Z")

</div>

In all likelihood it is the dodgy JSON.  
You have 3 "must" clauses and JSON parsers will typically not error when this happens but take the last of the values for the duplicate keys (so the value { bool: {} } ). The first "must" value with the useful looking criteria will be ignored

---

<div class="post-metadata">

**Author:** ![beenoisy](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@beenoisy](https://discuss.elastic.co/u/beenoisy)\
**Post date:** [December 1, 2016, 1:08pm UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/3 "2016-12-01T13:08:19Z")

</div>

First of all, I did doubt the three must block. But, even if I try to use one block only. Not work too.

one block like:

```auto

{
           "filter":{
               "bool":{
                   "must":{
                       "term":{"authorDept2": "工程生产力部"}
                   }
               }
           }
       }

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 1, 2016, 1:15pm UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/4 "2016-12-01T13:15:44Z")

</div>

You can debug why your doc is not matching using the explain API [1]  
It will likely be a mismatch between the un-tokenized query (untokenized because you used the `term` query). If you use the `match` query instead of the `term` query you can normally rely on your query terms matching what ever tokenization policy is used to index your content.

You can debug what tokens are being created in your index using the analyze API [2]

[1] [https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-explain.html)  
[2] [https://www.elastic.co/guide/en/elasticsearch/reference/5.0/\_testing\_analyzers.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/_testing_analyzers.html)

---

<div class="post-metadata">

**Author:** ![beenoisy](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@beenoisy](https://discuss.elastic.co/u/beenoisy)\
**Post date:** [December 5, 2016, 8:25am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/5 "2016-12-05T08:25:18Z")

</div>

I try to use `explain` api to find out what's wrong in my query, but there is no more useful message there.

the full output:

```auto

{
    "_index": "ugc",
    "_type": "article",
    "_id": "_explain",
    "_version": 2,
    "found": true,
    "_source": {
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "must": {
                            "bool": {
                                "must": {
                                    "term": {
                                        "authorDept2": "工程生产力部"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

```

On the other side, All I want to do is make a filter query which use multi filed. Like a logic statement:

```auto

filedA = 1 AND (filedB = "some word" OR fieldC = "some value")

```

Is there a best way for me ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 5, 2016, 9:38am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/6 "2016-12-05T09:38:38Z")

</div>

> [@beenoisy](#):
>
> I try to use explain api to find out what's wrong in my query

That looks like you've created a document in your index with the ID "\_explain" containing the source of a query.  
What version of elasticsearch are you on and what command did you issue to get to this point?

---

<div class="post-metadata">

**Author:** ![beenoisy](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@beenoisy](https://discuss.elastic.co/u/beenoisy)\
**Post date:** [December 5, 2016, 9:50am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/7 "2016-12-05T09:50:54Z")

</div>

My `elasticsearch` version is `2.4.1`.  
Thank for notice me that I have created a document that ID is `_explain`. It's my mistake.

I am a new bee on elasticsearch. In this issue, I want to create a query which can filter data by logic statement.

For example:  
If there is 5 filed in my `es`: Fild1, Fild2, Fild3, Fild4, Fild5. Field1 ~ Field5 maybe contains some `Chinese` or `Japenese` character. I use `ik` analysis to cut word and do analysis.

I want to search data from es by filter like:

> (Field1 = 1 `OR` Fidle2 = "some text") AND (Field3 = "some text" `OR` Field4 = 10002 `OR` Field5 = "some content")

And, I want to know what should I do.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 5, 2016, 9:54am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/8 "2016-12-05T09:54:56Z")

</div>

> [@beenoisy](#):
>
> I want to search data from es by filter like:

Roughly:

```
 bool
    must
        bool
            should
                field1:some text
                field2: some text
        bool
            should
                field3: some text
                field4: 10002
                field5: some content

```

See [Boolean Operations and Filters in the Bool Query in Elasticsearch | Elastic Blog](https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query)

---

<div class="post-metadata">

**Author:** ![beenoisy](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@beenoisy](https://discuss.elastic.co/u/beenoisy)\
**Post date:** [December 5, 2016, 9:57am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/9 "2016-12-05T09:57:34Z")

</div>

Thanks for you reply, Give me some time to read this article. I'ill try what this post say.

---

<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:** [January 2, 2017, 9:57am UTC](https://discuss.elastic.co/t/is-there-some-wrong-usage-of-my-query-json/67736/10 "2017-01-02T09:57:38Z")

</div>

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