Select document from Elasticsearch

Hello all,

I have a problem when select document.

{
  "database" : {
    "mappings" : {
      "tables" : {
        "properties" : {
          "sex" : {
            "type" : "string"
          },
          "name" : {
            "type" : "string"
          },
          "status" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

In my indexes, i have full records

  • sex
  • name
  • status

Now i want search all documents have:

(status = 0) AND ((sex = male) OR (name = Long))

I already use Bool Query but not success. Demo here:

'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    'term' => [
                        'status' => 0
                    ]
                ],
                'should' => [
                    [ 'term' => [ 'sex' => 'male']],
                    [ 'term' => [ 'name' => 'Long']],
                ]
            ]
        ]
    ]

Anyone can resolve this problem help me?

Hi,

Can you please provide the mappings?

If you use default analyzers there is a chance that the name is analyzed - you can try to use a match query instead of a term query for this field.

Thank you @tanguy

My mapping:

{
    "_index" : "db-20161108",
    "_type" : "tabless",
    "_id" : "AVhBnmmBxTcOFtA9xu-J",
    "_score" : 1.0,
    "_source" : {
    	"time" : "2016-11-06 04:45:33",
        "ip" : "58.187.44.33",
        "asn" : "12345",
        "status" : "0",
        "source" : "ips"
    }
}

My code edited here but it show incorrect data:

'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    'match' => [ 'status' => "0"],
                ],
                'should' => [
                    [ 'match' => [ "asn" => "12345"]],
                    [ 'match' => [ "ip" => "58.187.44.33"]],
                    [ 'match' => [ 'ip' => '34.55.245.23']],
                ]
            ]
        ]
    ]

Thank you.

I think your query is not right.
if you have must clauses in boolquery, then should clauses are not required.
So in this case, all documents with status 0 are returned.
If that's right,
may be you can try: boolquery.minimumNumberShouldMatch
or may be you can use bool[must status,must[bool[should sex, should name]]]

I'm not sure if the first choice works, but you can try.

BTW: how to input a code box like that in your post?... two `` is just a simple format

Thank you @chenjinyuan87

Yes. It return all document with status = 0.

I use this query below is correct? Can you help me query?

'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    'match' => [ 'status' => "0"],
                ],
                'should' => [
                    [ 'match' => [ "asn" => "12345"]],
                    [ 'match' => [ "ip" => "58.187.44.33"]],
                    [ 'match' => [ 'ip' => '34.55.245.23']],
                ]
            ]
        ]
    ]

--

I put my code into box: choose code -> click "</>" in menu. :D.

Do you have Skype?

It's similar with the previous one.
I think you need to read the documents about booleanquery in lucene carefully, so that you can understand what's
must clause, and what's should, mustnot.
booleanquery is not logistic and/or. It's something a little different.

The simplest usage of booleanquery is:
in each query, all the clauses are must or all should.
in this way, it works like and, or.
The query with only must clauses is A AND B AND C ..
and with should is A OR B OR C..

and than you can combine several bools together.

If you want to use must and should together in one query, you must understand the mechanism of booleanquery clearly. Otherwise, you just make some mistake..

We don't use skype in China, sorry.

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