Search code examples
javalucene

Java Lucene How can i find class TermFreqVector and class TermPositionVector?


My IDE don't see this interface and i don't understand how i can use it.

(Windows+NetBeans+Lucene 7.4.0)

My pom.xml:

<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.javacodegeeks</groupId>
    <artifactId>jstringsearch</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>jstringsearch</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <java.version>1.8</java.version>
        <lucene.version>7.4.0</lucene.version>
    </properties>

    <!-- Build plugins -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.javacodegeeks.jstringsearch.Main2</mainClass>
                       
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
                      
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.javacodegeeks.jstringsearch.Main2</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
    
    <dependencies>
      
        <!-- Full text search -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>${lucene.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>${lucene.version}</version>
            <type>jar</type>
        </dependency>
        <!-- To include highlight support -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-highlighter</artifactId>
            <version>${lucene.version}</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
      
        <!-- Detect languages of text -->
        <dependency>
            <groupId>io.github.kju2.languagedetector</groupId>
            <artifactId>language-detector</artifactId>
            <version>1.0.6-SNAPSHOT</version>
            <type>jar</type>
        </dependency>

        <!-- Helps to create test units -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    
    </dependencies>
    
</project>

Part of code:

    Directory directory = new RAMDirectory();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
    IndexWriter writer = new IndexWriter(directory, indexWriterConfig);

    Document doc = new Document();
    // Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES
    FieldType type = new FieldType();
    type.setStoreTermVectors(true);
    type.setStoreTermVectorPositions(true);
    type.setStoreTermVectorOffsets(true);
    type.setStored(true);
    type.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    Field fieldStore = new Field("tags", "Kite good world.", type);
    doc.add(fieldStore);
    writer.addDocument(doc);
    writer.close();
    
    DirectoryReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    
    QueryParser queryParser = new QueryParser("tags", new StandardAnalyzer());
    Query query = queryParser.parse("\"Kite World\"~1");
    TopDocs results = searcher.search(query, 1);
    
    for ( ScoreDoc scoreDoc : results.scoreDocs) {

        //Here Red Errors!!!
        IndexReader re = DirectoryReader.open(directory);
        TermFreqVector tfvector = re.getTermFreqVector(scoreDoc.doc, "tags");
        TermPositionVector tpvector = (TermPositionVector) tfvector;
        int index = termPositionVector.indexOf("the");
        

In tutorials i see that someone just write:

import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.index.TermPositionVector;

But when i done this, my IDE don't looking for that, and it show me errors.


What do u thisnk about it? How can i find class TermFreqVector and class TermPositionVector?


Solution

  • TermFreqVector and TermPositionVector are very old Lucene code and it was present only until version 3.x.x.

    Most probably, your lucene version in the POM file is much more recent (8.x.x), so you have to adjust it.

    I suggest you to use 8.x.x and fix your code.