如何在elasticsearch中的bool查询中放置多个匹配查询

时间:2018-04-21 04:55:00

标签: spring-boot elasticsearch java-api

我正在尝试在弹性搜索查询下面实现

**- A full example of POM file.**
Command to Build All **Jrxml** to **Jasper File** in maven
If you used eclipse then right click on the project and Run as maven Build and add goals antrun:run@compile-jasper-reports 
compile-jasper-reports is the id you gave in the pom file.

**<id>compile-jasper-reports</id>**
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test`jasper</groupId>
  <artifactId>testJasper</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>TestJasper</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>6.3.0</version>
    </dependency>
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports-fonts</artifactId>
        <version>6.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.4.6</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.6</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
    <build>
    <pluginManagement>
    <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>       
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>compile-jasper-reports</id>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <target>
                            <echo message="Start compile of jasper reports" />
                            <mkdir dir="${project.build.directory}/classes/reports"/>
                            <echo message="${basedir}/src/main/resources/jasper/jasperreports" />
                            <taskdef name="jrc" classname="net.sf.jasperreports.ant.JRAntCompileTask"
                                classpathref="maven.compile.classpath" />
                                <jrc srcdir="${basedir}/src/main/resources/jasper/jasperreports" destdir="${basedir}/src/main/resources/jasper/jasperclassfile"
                                  xmlvalidation="true">
                                <classpath refid="maven.compile.classpath"/>
                                <include name="**/*.jrxml" />
                            </jrc>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </pluginManagement>
</build>
</project>

}

如何在java中实现它。 我想过这样做

{
"query": {
    "bool": {
        "must": [
            {"match": {"collegeName": "XXXX"}},
            {"match": {"userType": "STUDENT"}},
            {"match": {"details.blocklisted":"TRUE"}}
            ]
    }
}

有更好的方法吗???? 帮助!

2 个答案:

答案 0 :(得分:0)

是,

使用搜索模板(ES official doc)。它就像一个存储过程。具有可替换变量的查询。另外,更改查询只是一个简单的更新。事先创建模板(POST / _search / template / templateName {BODY})然后你可以调用它(POST / twitter / tweet / _search / templateName {包含动态变量的BODY})。

同样在JAVA中

Map<String, Object> params = new HashMap<String, Object>();
params.put("search_term", "elasticsearch");
params.put("since", "now-30d");

Template template = new Template("tweets", ScriptService.ScriptType.FILE, 
MustacheScriptEngineService.NAME, null, params);
SearchRequestBuilder request = 
client.prepareSearch(INDEX).setTemplate(template);

SearchResponse response = request.execute()。actionGet();

答案 1 :(得分:0)

您可以使用MultiMatchQueryBuilder之类的

SearchQuery searchQuery = new NativeSearchQueryBuilder()
              .withQuery(multiMatchQuery("college")
                .field("collegeName")
                .field("userType")
                .type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
              .build();