Elasticsearch: How to use two different multiple matching fields?

后端 未结 2 1147
梦毁少年i
梦毁少年i 2021-02-10 04:15

I want to do something much like the \'and\' filter example except with terms with a \'should\' in each one, instead of the field types in the example. I came up with the follow

2条回答
  •  半阙折子戏
    2021-02-10 04:44

    I think you don't need to specify bool filter. If I understand right what you are trying to accomplish, terms filter with and filter should be enough. So something like this:

    # Delete index
    #
    curl -s -X DELETE 'http://localhost:9200/bool-filter-test' ; echo
    
    # Create index
    #
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/' -d '{
      "mappings": {
        "document": {
          "properties": {
            "color": {
              "type": "string",
              "index": "not_analyzed"
            },
            "fruit": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }' ; echo
    
    # Index some documents
    #
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/1?pretty=true' -d '{
      "fruit" : "apple",
      "color" : "red"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/2?pretty=true' -d '{
      "fruit" : "apple",
      "color" : "yellow"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/3?pretty=true' -d '{
      "fruit" : "apple",
      "color" : "green"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/4?pretty=true' -d '{
      "fruit" : "banana",
      "color" : "green"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/5?pretty=true' -d '{
      "fruit" : "banana",
      "color" : "yellow"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/6?pretty=true' -d '{
      "fruit" : "pear",
      "color" : "green"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/7?pretty=true' -d '{
      "fruit" : "pear",
      "color" : "yellow"
    }' ; echo
    
    curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/7?pretty=true' -d '{
      "fruit" : "pear",
      "color" : "red"
    }' ; echo
    
    
    # Refresh index
    #
    curl -s -XPOST 'http://localhost:9200/bool-filter-test/_refresh'; echo
    
    
    # This query should return only red apples and pears
    #
    curl -s -X POST 'http://localhost:9200/bool-filter-test/_search?pretty' -d '{
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "and" : [
          {
            "terms" : { "fruit" : ["apple", "pear"] }
          },
          {
            "terms" : { "color" : ["red"] }
          }
        ]
      }
    }'
    

    You can even specify execution to bool (which according to documentation) Generates a term filter (which is cached) for each term, and wraps those in a bool filter. The bool filter itself is not cached as it can operate very quickly on the cached term filters.

提交回复
热议问题